Posts
31
Comments
48
Trackbacks
0
Get return value from stored procedure

Recently we changed our DAC layer from using inline SQL to stored procedures in the database. On some of these SQL, we did record deletion (usually only 1 record), and we just execute it via IDBCommand.ExecuteNonQuery() and then check the return value to see how many records were affected (which should be 1) for verification that the query actually does something.

With the change to stored procedure, we just return 1 in the stored procedure if the delete is successful. However, the calling code then started to show these deletions as errors. Apparently ExecuteNonQuery only returns the number of affected rows on SELECT, INSERT and DELETE statements; for everything else it returns -1.  So I tried to figure out how to get a return value from a stored procedure.

Let's assume a simplistic stored procedure as follows:

ALTER PROC ReturnOnly
AS
BEGIN
      RETURN 5
END

You can't use ExecuteScalar to get the returned value, and ExecuteNonQuery will always return -1.  To get the value back, you need to add a return value parameter to the command.  The name of the parameter is not important.  The code to get the value returned by that procedure will be as follows:

private int ExecuteStoredProcedure(string storedProcedureName)
{
   SqlConnection connection = new SqlConnection(connectionString);
 
   // Command - specify as StoredProcedure
   SqlCommand command = new SqlCommand(storedProcedureName, connection);
   command.CommandType = CommandType.StoredProcedure;
 
   // Return value as parameter
   SqlParameter returnValue = new SqlParameter("returnVal", SqlDbType.Int);
   returnValue.Direction = ParameterDirection.ReturnValue;
   command.Parameters.Add(returnValue);
 
    // Execute the stored procedure
   connection.Open();
   command.ExecuteNonQuery();
   connection.Close();
 
   return Convert.ToInt32(returnValue.Value);
}

 

posted on Wednesday, September 17, 2008 11:57 AM Print
Comments
Gravatar
# re: Get return value from stored procedure
Saif Khan
12/22/2008 8:21 PM
There are much more detailed samples and explantions in the help file of the Data Access Library from MS.
Gravatar
# re: Get return value from stored procedure
billy
3/29/2009 11:47 PM
The detailed samples and explanations aren't nearly as clear and straightforward as this. Those writers need to understand how to add code that is relevant to the solution; far too often, those samples add extraneous code which obfusticates the whole exercise.
Gravatar
# re: Get return value from stored procedure
Yclept
6/11/2009 7:27 AM
ALTER PROC ReturnOnly
AS
BEGIN
SELECT 5
END
Gravatar
# re: Get return value from stored procedure
Ravi Goswami
6/18/2009 11:17 AM
Thanks a lot buddy!

I have spend my 4 hours to find the solution. I found it here.

Why I could not be able to find u in the morning.

Thanks

Ravi Goswami
Gravatar
# re: Get return value from stored procedure
Li
6/25/2009 3:29 PM
I love you. I wasted the past few hours trying to find this. It solved my problem. Wish Google would direct me here earlier.

Good Job.

Post Comment

Title *
Name *
Email
Url
Comment *  
News