SQLCMD replaces deprecated command line utilities ISQL (which is no longer available in SQL Server 2005) and OSQL. The latter is still supported but will be discontinued in future releases of software. You can use SQLCMD to run Transact-SQL statements, system procedures and scripts from the command line if client tools are not available. SQLCMD uses OLEDB for connecting to the server and executing statements. SQLCMD is available under SQL Server installation folder, by default this is C:\Program Files\Microsoft SQL Server\90\Tools\Binn. The following table explains SQLCMD switches; as with all other command-line tools switches are case sensitive:
For example, the following command exports the content of sys.dm_exec_query_stats into a file:
1.
C:\Program Files\Microsoft SQL Server\90\Tools\Binn>sqlcmd.exe -S my_server\my_instance-Q "SELECT * FROM sys.dm_exec_query_stats" -o c:\query_exec_stats.txt
The next statement lists all local servers:
1.
C:\Program Files\Microsoft SQL Server\90\Tools\Binn>sqlcmd.exe -L