Using Scripting Variables with sqlcmd

sqlcmd provides a means for utilizing variables within sqlcmd input files or scripts. These scripting variables can be assigned as sqlcmd parameters or set within the sqlcmd script. To illustrate the use of scripting variables, let’s change our previous backup example so that the database that will be backed up is passed as a variable. A new input file named c:\BackupDatabase.sql should be created, and it should contain the following command:

BACKUP DATABASE $(DatabaseToBackup) TO DISK = 'c:\$(DatabaseToBackup).bak'

The variable in the preceding example is named DatabaseToBackup. Scripting variables are referenced using the $( ) designators. These variables are resolved at the ...

Get Microsoft SQL Server 2014 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.