Learn more about SQL Server tools

mssqltips logo
giveaway
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 

Returning stored procedure parameter values to a calling stored procedure



By:
Overview

In a previous topic we discussed how to pass parameters into a stored procedure, but another option is to pass parameter values back out from a stored procedure.  One option for this may be that you call another stored procedure that does not return any data, but returns parameter values to be used by the calling stored procedure.

Explanation

Setting up output paramters for a stored procedure is basically the same as setting up input parameters, the only difference is that you use the OUTPUT clause after the parameter name to specify that it should return a value.  The output clause can be specified by either using the keyword "OUTPUT" or just "OUT". For these examples we are still using the AdventureWorks database, so all the stored procedures should be created in the AdventureWorks database.

Simple Output

CREATE PROCEDURE dbo.uspGetAddressCount @City nvarchar(30), @AddressCount int OUTPUT
AS
SELECT @AddressCount = count(*) 
FROM AdventureWorks.Person.Address 
WHERE City = @City

Or it can be done this way:

CREATE PROCEDURE dbo.uspGetAddressCount @City nvarchar(30), @AddressCount int OUT
AS
SELECT @AddressCount = count(*) 
FROM AdventureWorks.Person.Address 
WHERE City = @City

To call this stored procedure we would execute it as follows.  First we are going to declare a variable, execute the stored procedure and then select the returned valued.

DECLARE @AddressCount int
EXEC dbo.uspGetAddressCount @City = 'Calgary', @AddressCount = @AddressCount OUTPUT
SELECT @AddressCount

This can also be done as follows, where the stored procedure parameter names are not passed.

DECLARE @AddressCount int
EXEC dbo.uspGetAddressCount 'Calgary', @AddressCount OUTPUT
SELECT @AddressCount





More SQL Server Solutions











Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Notify for updates 


Get free SQL tips:

*Enter Code refresh code     



Wednesday, November 30, 2016 - 6:20:24 AM - Nils Jafner Back To Top

Hi

I'm having trouble to create a stored procedure where I want to do something like where ZipCode in (@ZipCodelist) could you help me with that ?

I can create the sp but the result is always empty

 

BR

Nils J

 

 


Friday, November 25, 2016 - 11:40:35 AM - myidealab Back To Top

Great Post!

I originally executed a second stored procedure (recipient of the output) within the first stored procedure. However, this would only allow for one dataset result in SSRS. This would force me to maintain multiple stored procedures that would create the same output parameters. Since my result set consisted of 3 dataset results, I could simply add a few lines of code to reference the first stored procedure (outputs the values for the parameter), and use it for each of the 3 procedures (resultant datasets).

Thanks.


Learn more about SQL Server tools