The following table provides an overview of commands that you can use from within the sqlcmd utility. For more information about these commands, see sqlcmd Utility in SQL Server 2008 Books Online.
|
Command
|
Description
|
|---|
|
go [count]
|
Executes any cached Transact-SQL statements. When specifying a value for count, the cached statements will be executed count times as a single batch. For example, the following example will execute the two statements twice:
1> Use MyDatabase
2> SELECT FirstName, LastName FROM Employee WHERE EmployeeID < 10
3> GO 2
|
|
reset
|
Clears the statement cache.
|
|
ed
|
Lets you edit the current or last executed statement batch. To edit the last executed batch, this command must be typed immediately after the last batch has executed.
|
|
!! <command>
|
Executes operating system commands. To execute an operating system command, start a line with two exclamation marks (!!) followed by the operating system command. For example, the following performs a directory listing of the current drive:
|
|
quit
- or -
exit
|
Exits sqlcmd immediately.
|
|
exit [(statement)]
|
Lets you use the result of a SELECT statement as the return value from sqlcmd.
The exit() statement with nothing between the parentheses executes everything preceding it in the batch, and then exits with no return value.
You can also include the exit parameter as part of a batch file. For example:
sqlcmd /Q "exit(SELECT COUNT(*) FROM '%1')"
|
|
:r <filename>
|
Parses Transact-SQL statements and sqlcmd commands from the file specified by filename into the statement cache. The file will be read and executed after a batch terminator (go) is issued.
|
|
:serverlist
|
Lists the locally-configured servers and the names of the servers broadcasting on the network.
|
|
:list
|
Prints the content of the statement cache.
|
|
:listvar
|
Displays a list of the scripting variables that are currently set.
|
|
:error <destination>[ filename | STDERR | STDOUT]
|
Redirects all error output to the file specified by filename, to the STDERR stream, or to the STDOUT stream. By default, error output is sent to STDERR.
|
|
:out <destination>[ filename | STDERR | STDOUT ]
|
Redirects all query results to the file specified by filename, to the STDERR stream, or to the STDOUT stream. By default, output is sent to STDOUT.
|
|
:perftrace <destination>[ filename | STDERR | STDOUT ]
|
Redirects all performance trace information to the file specified by filename, to the STDERR stream, or to the STDOUT stream. By default, performance trace output is sent to STDOUT.
|
|
:connect server_name[\instance_name] [-t timeout] [-U username [-P password] ]
|
Connects to an instance of SQL Server and closes the current connection.
|
|
:on error [ exit | ignore ]
|
Sets the action to be performed when an error occurs during script or batch execution.
When the exit option is used, sqlcmd exits with the appropriate error value.
When the ignore option is used, sqlcmd disregards the error and continues executing the batch or script. By default, an error message will be printed.
|
|
:help
|
Lists the sqlcmd commands and a short description of each command.
|