SQL Server Admin
T-SQL Articles

November 13, 2009

Executing a Stored Procedure using SQLCMD




Here’s how to execute a SQL Server Stored Procedure using SQLCMD. For demonstration purposes, I will be executing the ‘CustOrderHist’ Stored Procedure in the Northwind database. This procedure accepts the CustomerID and returns a list of orders placed by the Customer.

Open your Command Prompt. I have a named instance and use Windows Authentication, so I will be connecting to SQL Server using the following statement:

sqlcmd -S <ComputerName>\<InstanceName>

In my case, it is:    sqlcmd -S SUPROTIM-PC\SUPROTIM2

Now enter the following commands:

:Setvar CustomerID ALFKI
EXEC dbo.CustOrderHist $(CustomerID)
Go

The output is as shown below:

image


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter



 
  Feedback:

comments

2 Responses to "Executing a Stored Procedure using SQLCMD"
  1. ESTEBAN ALVINO Q. said...
    December 1, 2009 2:55 PM

    but, if you have more than a database
    ?

  2. Burtey said...
    January 19, 2010 1:59 AM

    How can i print the list of databases in my current instance using SQLCMD?

 

Copyright © 2009-2011 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions