Returning SQL Server 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





Comments For This Article




Wednesday, August 5, 2020 - 11:15:45 PM - Xinbao Back To Top (86242)
Thank you. This tutorial is very useful.

Thursday, September 5, 2019 - 7:52:14 AM - Sai Ampolu Back To Top (82250)

Thank You Very Much.This Helped me a lot.


Wednesday, December 13, 2017 - 5:30:50 AM - jeya Back To Top (73962)

It is very useful


Monday, October 30, 2017 - 7:48:33 AM - Lana Salikova Back To Top (68961)

 thank you

 

 


Saturday, June 24, 2017 - 1:13:43 AM - nikhil Back To Top (58059)

 Thanks for the wonderful article. I like it very much.

 


Thursday, June 8, 2017 - 2:15:19 AM - ANIRUDH Back To Top (56961)

 

 

Excellent tutorial....gREAT JOB


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

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 (43847)

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.















get free sql tips
agree to terms