Using SQLCmd to output results

SQL Management Studio is a great application, however there are times when SQL Server Management Studio (SSMS) is unavailable (either too slow to start, or simply not installed). However, there is a really useful application called sqlcmd which is installed with SQL Server. This is a command line application, located in C:Program FilesMicrosoft SQL Server90ToolsBinn (SQL Server 2005), you should find that this location has been set in your %PATH% environment variable meaning you can execute it from anywhere, which allows you to connect to a server, execute SQL commands and for the results to be outputted in the console window.

Below, I am connecting to my local SQL Express instance, and querying the Customers table in the Northwind database.  The results of the query, in this case the amount of rows in the table is displayed.

Z:>sqlcmd -S .SQLEXPRESS
1> USE Northwind
2> SELECT count(*) FROM Customers
3> GO
Changed database context to ‘Northwind’.

———–
       1000

(1 rows affected)
1>

It’s really quick and it’s great if you just want to execute a simple command and don’t want to wait 5 minutes for SSMS to load. A word of guidance, to execute the query you need to execute the GO statement, this will then execute everything in the batch.

Technorati Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *