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

2 comments:

  1. but, if you have more than a database
    ?

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

    ReplyDelete