Monthly Archives: August 2009

SQLCMD Startup Script

SQLCMD can run a startup script defined by the environment variable SQLCMDINI. Suppose you want to always execute the following query when you are connected to the server: SELECT @@SERVERNAME, @@VERSION Place this command inside the init.sql file and define … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

SQLCMD Scripting Variables

It’s a good idea to discuss scripting variables at this point before discussing different SQLCMD commands, primarily because certain commands reveal their true power only via leveraging scripting variables. SQLCMD variables are different from T-SQL variables in many aspects. The … Continue reading

Posted in Uncategorized | Tagged , | Leave a comment

:XML [ON/OFF] SQLCMD Command

:XML [ON/OFF] When you expect XML data to be coming back from the server, for example because of a FOR XML clause in your query, you should set XML mode to ON via this command, to properly format the output. … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

:ON ERROR [exit/ignore] SQLCMD Command

:ON ERROR [exit/ignore] This command instructs SQLCMD on how to handle errors during script execution. The following options are available: *      exit Exits the program with the appropriate error value *      ignore Ignores the error and continues execution. An error … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

:PERFTRACE /STDERR/STDOUT SQLCMD Command

ERFTRACE <filename>/STDERR/STDOUT The ERFTRACE command redirects all performance information (see the discussion of the -p option) that by default goes to standard output.

Posted in Uncategorized | Tagged , , | Leave a comment

:OUT /STDERR/STDOUT SQLCMD Command

:OUT <filename>/STDERR/STDOUT This command redirects all query results output that by default gets sent to standard output.

Posted in Uncategorized | Tagged , | Leave a comment

:ERROR /STDERR/STDOUT SQLCMD Command

:ERROR <filename>/STDERR/STDOUT This command enables the user to redirect error output, either to a specified file or to standard error/standard output console streams. You may want to refer back to command line option -r, discussed earlier in this chapter. Tips … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

:LISTVAR and :LIST SQLCMD Command

:LISTVAR and :LIST The LISTVAR command was mentioned earlier during the introduction of scripting variables. It lists currently defined variables and, as such, is handy in troubleshooting SQLCMD. There is a different command, LIST, which enables you to list contents … Continue reading

Posted in Uncategorized | Tagged , , , | Leave a comment

:SERVERLIST SQLCMD Command

:SERVERLIST This command corresponds to the command line option -L, listing all locally configured servers and the names of the servers broadcasting on the network. How does it help you to know the names of all servers when you are … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

:r SQLCMD Command

:r <filename> This option enables the script writer or interactive user to read the contents of a specified script file and include it in the current execution session. The included file is processed in-place; in other words, every command encountered … Continue reading

Posted in Uncategorized | Tagged , | Leave a comment