Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Return Value of a Stored Procedure

Probably an easy-to-answer question. I have this procedure:

CREATE PROCEDURE [dbo].[AccountExists]     @UserName nvarchar(16) AS IF EXISTS (SELECT Id FROM Account WHERE UserName=@UserName) SELECT 1 ELSE SELECT 0  

When I have ADO.NET code that calls this procedure and does this:

return Convert.ToBoolean(sproc.ExecuteScalar()); 

Either true or false is returned.

When I change the stored procedure to RETURN 1 or 0 instead of SELECT:

ALTER PROCEDURE [dbo].[AccountExists]     @UserName nvarchar(16) AS IF EXISTS (SELECT Id FROM Account WHERE UserName=@UserName) RETURN 1 ELSE RETURN 0  

sproc.ExecuteScalar() returns null. If I try sproc.ExecuteNonQuery() instead, -1 is returned.

How do I get the result of a stored procedure with a RETURN in ADO.NET?

I need AccountExists to RETURN instead of SELECT so I can have another stored procedure call it:

--another procedure to insert or update account  DECLARE @exists bit  EXEC @exists = [dbo].[AccountExists] @UserName   IF @exists=1 --update account ELSE  --insert acocunt 
like image 314
core Avatar asked Apr 14 '09 22:04

core


People also ask

Can stored procedure return value?

A stored procedure does not have a return value but can optionally take input, output, or input-output parameters. A stored procedure can return output through any output or input-output parameter.

Does procedure returns a value SQL?

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.

Can we return data from stored procedure in SQL?

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.

How do you store a return value of a stored procedure in a variable?

You can use the return statement inside a stored procedure to return an integer status code (and only of integer type). By convention a return value of zero is used for success. If no return is explicitly set, then the stored procedure returns zero. You should use the return value for status codes only.


2 Answers

Add a parameter, using ParameterDirection.ReturnValue. The return value will be present in the paramter after the execution.

like image 158
John Saunders Avatar answered Oct 07 '22 18:10

John Saunders


Also, to retrieve the result (or any other output parameter for that matter) from ADO.NET you have to loop through all returned result sets first (or skip them with NextResult)

This means that if you have a procedure defined like this:

CREATE PROC Test(@x INT OUT) AS     SELECT * From TestTable     SELECT @x = 1 

And try to do this:

SqlCommand cmd = connection.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Test" cmd.Parameters.Add("@x", SqlDbType.Int).Direction = ParameterDirection.Output; cmd.Parameters.Add("@retval", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;  cmd.Execute(); int? x = cmd.Parameters["@x"].Value is DBNull ? null : (int?)cmd.Parameters["@x"].Value; 

Then x will contain null. To make it work, you have to execute the procedure like:

using (var rdr = cmd.ExecuteReader()) {     while (rdr.Read())         MaybeDoSomething; } int? x = cmd.Parameters["@x"].Value is DBNull ? null : (int?)cmd.Parameters["@x"].Value; 

In the latter case, x will contain 1 as expected.

like image 40
erikkallen Avatar answered Oct 07 '22 17:10

erikkallen