Justin Randall jrandall@sqlsentry.com SQLintersection Session: Friday, 10:00am-11:15pm Automating SQL Server Administration Using SQLCMD Justin Randall.

Slides:



Advertisements
Similar presentations
Visit : Call Us: US: , India:
Advertisements

Visit : Call Us: US: , India:
Week 6: Chapter 6 Agenda Automation of SQL Server tasks using: SQL Server Agent Scheduling Scripting Technologies.
Automating Common DBA Tasks
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
AGENDA Tools used in SQL Server 2000 Graphical BOL Enterprise Manager Service Manager CLI Query Analyzer OSQL BCP.
COMMANDLINE OPTIONS IN SSIS -ABHIJIT -SANJAY -SUSHANT.
Module 3: SQL Server 2005 Administrative Tools. Overview Using SQL Server Management Studio Using SQL Computer Manager Using the sqlcmd Utility Using.
A Technical Overview of Microsoft SQL Server 2005 Melville Thomson IT Pro Evangelist (UK)
Slide 1 of 9 Presenting 24x7 Scheduler The art of computer automation Press PageDown key or click to advance.
Module 13 Automating SQL Server 2008 R2 Management.
Module 2: Using Transact-SQL Querying Tools. Overview SQL Query Analyzer Using the Object Browser Tool in SQL Query Analyzer Using Templates in SQL Query.
Maintaining a Microsoft SQL Server 2008 Database SQLServer-Training.com.
1.1 SQL Server 2005 管理工具介紹與使用. Overview Using SQL Server Management Studio Using SQL Configuration Manager Using the sqlcmd Utility.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
Chapter 9 Scripting RMAN. Background Authors felt that scripting was a topic not covered well Authors wanted to cover both Unix/Linux and Windows environments.
Informix IDS Administration with the New Server Studio 4.0 By Lester Knutsen My experience with the beta of Server Studio and the new Informix database.
SUS Commander Sean Merritt. Background Department of Natural Resources uses a Software Update Server to update the user’s PCs. The log files are cryptic.
SQL Server User Group Meeting Reporting Services Tips & Tricks Presented by Jason Buck of Custom Business Solutions.
Learningcomputer.com SQL Server 2008 – Administration, Maintenance and Job Automation.
IT 456 Seminar 5 Dr Jeffrey A Robinson. Overview of Course Week 1 – Introduction Week 2 – Installation of SQL and management Tools Week 3 - Creating and.
Database control Introduction. The Database control is a tool that used by the database administrator to control the database. To enter to Database control.
1 Chapter Overview Preparing to Upgrade Performing a Version Upgrade from Microsoft SQL Server 7.0 Performing an Online Database Upgrade from SQL Server.
A Brief Documentation.  Provides basic information about connection, server, and client.
1 Chapter Overview Planning to Install SQL Server 2000 Deciding SQL Server 2000 Setup Configuration Options Running the SQL Server 2000 Setup Program Using.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
3 Copyright © 2006, Oracle. All rights reserved. Using Recovery Manager.
Batch Jobs Using the batch job functions. Use [Bulk Changes][Batch Job Utility] to start. Read the information panel. Check with TAMS Technical Support.
1 Introduction to SQL *Plus Oracle SQL Interface MIS309 Database Systems.
Oracle Query VBA Tool (OQVT)
Rob Sewell Making PowerShell Useful Real-Life Examples of Powershell in Action Slides available here -
Dealing with Database Corruption DBA 911. Who am I? 2 David M Maxwell twitter.com/dmmaxwell or twitter.com/upsearchsqltwitter.com/dmmaxwelltwitter.com/upsearchsql.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
1 Build Your Own MySQL Time Machine Chuck Bell, PhD Mats Kindahl, PhD Replication and Backup Team Sun Microsystems 1.
Dept. of Computer & Information Sciences
SQL Database Management
Fundamental of Databases
Managing, Storing, and Executing DTS Packages
, MS-Access, QBE, Access/Oracle
Automated Restore Script Output for Ola Hallengren’s Maintenance Solution 1) Start SQL Services on Local 2) Connect to Azure AlwaysOn 3) Delete all maintenance.
A Guide to SQL, Seventh Edition
Introduction to Microsoft SQL Server 2016
SQL and SQL*Plus Interaction
Using SQL*Plus.
Achieve more in less time using the new SQL PowerShell
PowerShell is Happening FOR REAL THIS TIME!!!
Example of a page header
Using SQL Server through Command Prompt
Deploying and Configuring SSIS Packages
Shell Script Assignment 1.
Contained DB? Did it do something wrong?
1z0-320 Exam dumps - Get 1z0-320 PDF With Actual Questions Answers
Automating SQL Server Management
Using SQL*Plus.
Module 1 Introduction to Microsoft SQL Server 2016
Lecture 1: Introduction
SQL Server and PowerShell Let’s Get Serious
Making PowerShell Useful
Alternate Version of STARTING OUT WITH C++ 4th Edition
PowerShell for Data Professionals
Making PowerShell Useful
8 6 MySQL Special Topics A Guide to MySQL.
Using SQL*Plus.
NAVIGATING THE MINEFIELD
Governing Your Enterprise with Policy-Based Management
Disaster Recovery Done Dirt Cheap Founder Curnutt Data Solutions
Ch 10. Maintaining and Automating SQL Server
Module 1 Introduction to Microsoft SQL Server 2017
The DBA Quit and now you’re it:
Presentation transcript:

Justin Randall jrandall@sqlsentry.com SQLintersection Session: Friday, 10:00am-11:15pm Automating SQL Server Administration Using SQLCMD Justin Randall jrandall@sqlsentry.com

Speaker: Justin Randall Senior Consultant Working with data and databases since 1985 First Sybase SQL Server project: 1997 First Microsoft SQL Server project: 1999, version 7.0

Overview Introduction Execution Options Uses The sqlcmd utility: a command-line tool for submitting T-SQL statements, system procedures, and script files. Connect to local and remote instances Read and Write Text files Execution Options SSMS Query Editor, Windows Command Prompt, SQL Server Agent Uses automating any repetitive tasks, i.e. batch processing, unit testing solid alternative to SSIS for low to medium complexity tasks Sqlcmd makes many SQL Server tasks, such as automating test runs and maintenance tasks, easier and quicker. The sqlcmd command-line utility is valuable for executing batches of SQL Statements to SQL servers, and saving results to file.   The sqlcmd utility in SQL Server is a command-line tool that lets you submit T-SQL statements or batches to local and remote instances of SQL Server. The utility is extremely useful for repetitive database tasks such as batch processing or unit testing. It also provides an easy way of simulating load to a database under development.

Execution Options SSMS Query Editor in SQLCMD Mode Command Prompt Interactive or script file Command Prompt Interactive or single command SQL Server Agent CmdExec Job Step Ola Hallengren's Maintenance Solution

Demo Execution Options Run AutomateBackups script in SSMS (sqlcmd mode) Run same query in Windows command line – output to AutoBackupResults.txt sqlcmd –E –S .\SQLServer2014 –d master –i D:\SQLDemos\AutomateBackup.sql –o D:\SQLDemos\AutoBackupResults.txt Run as SQLAgent job

Syntax & Elements Command Line Options Scripting Variables Commands Login-Related Options, Input/Output Options, Query Execution Options, Formatting Options, Error Reporting Options, Miscellaneous Options Scripting Variables Commands Editing Commands, Variables, Output Commands, Execution Control Commands, Miscellaneous Commands Take a look at a sampling of options, variables, and commands

Useful Command Line Options -A (dedicated administrator connection) -b (terminate batch job if there is an error -d Database name -E (use trusted connection) -i input file (path and filename) -o output file (path and filename) -q "command line query; -Q "command line query" (and exit) -S server[\instance name] [,port] -U login id; -P password

Demo Command Line Options sqlcmd –U test –P test –S . –d WideWorldImportersLegacy –q "SELECT CAST(c.CustomerName AS nvarchar(35)), CAST(EmailAddress AS nvarchar35)) From Sales.Customers c Join Application.People p on c.PrimaryContactPersonID = p.PersonID Where c.CreditLimit > 3000" sqlcmd –U test –P test –S . –d WideWorldImportersLegacy –i D:\SQLDemos\inputscript.sql sqlcmd –E –S . –d WideWorldImportersLegacy –i D:\SQLDemos\inputscript.sql sqlcmd –E –S . –d WideWorldImportersLegacy –i D:\SQLDemos\inputscript.sql –o D:\SQLDemos\ouputscript.txt

Using Variables Set variables externally and pass them into a script file sqlcmd –E –S. –i"D:\SQLCMDS\script1.sql" –v var1=value1 var2=value2 Interactive mode: SET variable, call from sqlcmd C:\>SET tablename=sys.databases sqlcmd –d master SELECT name, database_id from $(tablename) Set variables inside the SQLCMD script in SSMS :SETVAR DbName = "AdventureWorks2012" USE $(DbName);

Demo Scripting Variables 1. C:\>sqlcmd –E –S .\SQLServer2014 –d AdventureWorks2014 –v LastName="Adams" TableName=Person.Person –i "D:\SQLDemos\Variables.txt" 2. SET TableName=Person.Person SET LastName="Adams" sqlcmd -E –S .\SQLServer2014 –d AdventureWorks2014 Select FirstName +' ' + LastName From $(TableName) Where LastName = $(LastName)

Commands :!![<command>] – Execute a command in the Windows command shell executed on the computer on which sqlcmd is running run immediately not all Windows command shell commands work :connect – to a local or remote SQL Server instance :serverlist :setvar <variable> <value> :help :quit or :exit

Demo Commands Run script file:

Automation Automate any number of repetitive tasks that require: file creation / deletion connecting to multiple servers other Windows command prompt commands Read/Write data in SQL Server Examples: Backup-Restore Log Shipping set up Business tasks

Demo Automation

References Books Online: sqlcmd Utility https://technet.microsoft.com/en-us/library/ms162773(v=sql.130).aspx Book Online: Using the sqlcmd Utility https://technet.microsoft.com/en-us/library/ms180944(v=sql.130).aspx sqlcmd Basics https://www.simple-talk.com/sql/sql-tools/sql-server-sqlcmd-basics/ Deploying Scripts with sqlcmd http://www.sqlservercentral.com/articles/SQLCMD/66183/

Review Introduction Execution Options Uses The sqlcmd utility: a command-line tool for submitting T-SQL statements, system procedures, and script files. Connect to local and remote instances Read and Write Text files Execution Options SSMS Query Editor, Windows Command Prompt, SQL Server Agent Uses automating any repetitive tasks, i.e. batch processing, server configuration, etc. solid alternative to SSIS for low to medium complexity tasks

Questions? Don’t forget to complete an online evaluation! Automating SQL Server Administration Using SQLCMD Your evaluation helps organizers build better conferences and helps speakers improve their sessions. Thank you!

2017 Save the Date! May 21-24 www.SQLintersection.com We’re back in Orlando! Leave the every day behind and enter a world of wonder and enchantment at the Walt Disney World® Resort. Located in the heart of the most magical place on earth, the Walt Disney World Swan and Dolphin Resort provides a truly extraordinary backdrop for our event! Beautiful tropical landscaping, tranquil waterways and classic art and architecture work together to create a stunning landmark!

Final Comments You should always have a review slide that “mirrors” the Overview slide If you want to have references that you describe in detail, then we suggest you use a: References slide (or more than one) Then, your review slide Then, a Questions? / Thank You! slide at the end Otherwise, use: Your review slide You can change this up to suit your style (maybe references after review) but always have a review slide and the Questions? / Thank You! slide