Download presentation
Presentation is loading. Please wait.
Published byHannah Blankenship Modified over 6 years ago
1
Justin Randall jrandall@sqlsentry.com
SQLintersection Session: Friday, 10:00am-11:15pm Automating SQL Server Administration Using SQLCMD Justin Randall
2
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
3
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.
4
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
5
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
6
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
7
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
8
Demo Command Line Options
sqlcmd –U test –P test –S . –d WideWorldImportersLegacy –q "SELECT CAST(c.CustomerName AS nvarchar(35)), CAST( Address 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
9
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);
10
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 AdventureWorks Select FirstName +' ' + LastName From $(TableName) Where LastName = $(LastName)
11
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
12
Demo Commands Run script file:
13
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
14
Demo Automation
15
References Books Online: sqlcmd Utility
Book Online: Using the sqlcmd Utility sqlcmd Basics Deploying Scripts with sqlcmd
16
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
17
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!
18
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!
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.