Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get RETURN value from stored procedure in SQL

Tags:

sql

sql-server

I have a stored procedure where it ends with a RETURN value of 0 or 1.

I want to use this value in an IF statement in another stored procedure.

How can I get the return value of the former stored procedure and save it in a variable in the latter?

I couldn't find anything related. All questions are about fetching the RETURN values in C#.

I was thinking, maybe something like this :

SP_Two

DECLARE @returnValue INT SET @returnValue = EXEC SP_One  IF @returnValue = 1 BEGIN    --do something END ELSE BEGIN    --do something else END 
like image 353
hermann Avatar asked Nov 28 '12 12:11

hermann


People also ask

Can SQL stored procedure return value?

Return Value in SQL Server Stored ProcedureIn default, when we execute a stored procedure in SQL Server, it returns an integer value and this value indicates the execution status of the stored procedure. The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.

How can I return a single value from a stored procedure in SQL Server?

SQL Server allows to return a single integer value from a Stored Procedure using the RETURN keyword. The Return keyword is supported in Stored Procedures of all SQL Server versions i.e. 2000, 2005, 2008, 2008R2, 2012 and 2014.

Can we use return in stored procedure?

You can use one or more RETURN statements in a stored procedure. The RETURN statement can be used anywhere after the declaration blocks within the SQL-procedure-body. To return multiple output values, parameters can be used instead. Parameter values must be set before the RETURN statement runs.


1 Answers

This should work for you. Infact the one which you are thinking will also work:-

 .......  DECLARE @returnvalue INT   EXEC @returnvalue = SP_One  ..... 
like image 66
Rahul Tripathi Avatar answered Oct 26 '22 22:10

Rahul Tripathi