Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return value of a Stored Procedure

Stored Procedures

Return value of a Stored Procedure

by  dbtech  Posted    (Edited  )
Passing parameters from one stored procedure to another

Example 1
Popular!
Code:
/*Call procedure*/
create  procedure proc1 
as
/*Declare the output parameter*/
declare @result int
exec proc2 @result output
print @result
go

/*Actual procedure*/
Create procedure proc2 @paramout int output
as
/*Do something*/
SET @paramout=1
go
exec proc1
------------------------------------------------
Example 2
This makes use of the RETURN statement
Code:
create procedure usp_generateid @Addrid int,@hubid int
as
/*Do something */
Declare @outaddr int
IF @addriD>@hubid
BEGIN
    SET @outaddr=999
END
ELSE
BEGIN
    SET @outaddr=888
END
RETURN @outaddr /*Return the value using RETURN */
GO
/*Call Procedure*/
DECLARE @hubid int,
        @addrrid int,
        @OutAddrId int 
/*need to declare @OutAddrId this holds the result*/

/*Assign values */
SELECT @addrrid=11,@hubid=9
EXECUTE @OutAddrId = usp_generateid @addrrid,@HubId
PRINT @OutAddrId

Note when u use the example 2
1>u can return only one value of integer type
2)CANNOT RETURN NULL VALUES, ONLY INTEGER, error message will be returned
2>Multiple values cannot be return
3>Mainly used for returning status of stored procedures or error codes

Praveen
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top