norris1023
norris1023

Reputation: 633

Using SQLCMD in sql server

This might be a stupid question but I am learning about SQLCMD and the code for it. I don't really understand where to put the statement in.

This is what I am suppose to do. Use SQLCMD to output the results of the query SELECT COUNT(*) FROM Customers to the console window.

I have never done this before so I am thrown off a little. If someone would be able to help me understand it that would be great. I have done web searches and reviewed my book but it doesnt say where you will type the code into and I am working with SQL server 2008.

Upvotes: 2

Views: 2606

Answers (2)

zloctb
zloctb

Reputation: 11194

C:\Program Files\Microsoft SQL Server\110\Tools\Binn>sqlcmd -Q "use YourDB"
 C:\Program Files\Microsoft SQL Server\110\Tools\Binn>sqlcmd -Q "Select * From YourDB.dbo.param1"
id          num
----------- -----------
          3           1
          2           2
          3           3

Upvotes: 0

user596075
user596075

Reputation:

I think the best way to show how it is used is with documentation. You can see the SQLCMD Reference here.

You use sqlcmd to execute Transact-SQL against an instance\database.

This is untested, but your example your desired command would be something like: sqlcmd -U <yourLogin> -Q "select count(*) from Customers" -S <yourMachineName>\<yourInstanceName>

After you hit "ENTER", you will be prompted for your password (this is provided you are using SQL Auth), and then upon authentication you will see the result of your query on the console window.

Upvotes: 3

Related Questions