Posts
56
Comments
307
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);
}

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
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.
Gravatar
# re: Get return value from stored procedure
Eternal Bard
8/12/2009 12:34 PM
Thanks a lot, really usefull.
Gravatar
# re: Get return value from stored procedure
Dimitrij Albocha
8/31/2009 9:39 PM
Thank you very much for your solution! It works really.
Gravatar
# re: Get return value from stored procedure
harshada
11/5/2009 12:25 AM
code helps me lot !!! thanks.
Gravatar
# re: Get return value from stored procedure
andre
12/5/2009 10:47 PM
finally someone who makes it clear!
thanks
Gravatar
# re: Get return value from stored procedure
GaryHumf
2/15/2010 4:25 PM
This looks like the answer to a problem that has been way to hard to solve. I am using exec in a stored procedure to run parameter driven SQL just to get a record count. Do you have the equivalent in VB?
Gravatar
# re: Get return value from stored procedure
Pruthvi
2/25/2010 10:14 PM
can someone tell me how to grab multiple rows returned by sproc?
Gravatar
# thanks
heni
4/20/2010 9:48 PM
this code helps me lotssss
thanks
Gravatar
# re: Get return value from stored procedure
vinay
4/26/2010 8:40 PM
thanks dear
Gravatar
# re: Get return value from stored procedure
sam
6/2/2010 6:00 AM
thanks a lot buddy
Gravatar
# re: Get return value from stored procedure
krutarthp
8/12/2010 1:20 AM
Good and Clear Example. Thanks a lot.
Gravatar
# re: Get return value from stored procedure
PainBringer
11/17/2010 12:35 AM
Hey man, this is great. Exactly what I've been looking for.
Gravatar
# re: Get return value from stored procedure
ankit garg
11/27/2010 9:18 PM
thnx a lot...
this code really helps
Gravatar
# re: Get return value from stored procedure
Rajalingam
12/15/2010 7:25 PM
Thank u very much.

This code very useful to me
Gravatar
# re: Get return value from stored procedure
wardeaux
1/28/2011 1:59 AM
"Righteous! RIGHTEOUS!!"
--Crush "Finding Nemo"
Gravatar
# re: Get return value from stored procedure
Hemant Ghaydar
3/21/2011 9:49 PM
I Love You. This Solve Problem Very Fast
Gravatar
# Thanks
ankit
9/4/2011 12:47 AM
Thnaks Nice & Neat
and above all simple & clear
Gravatar
# re: Get return value from stored procedure
Goku
9/7/2011 7:33 AM
where put the return value?
Gravatar
# re: Get return value from stored procedure
sudhir
9/14/2011 7:25 AM
Thanks a lot, I spend almost 2 hrs to find the solution...
Gravatar
# re: Get return value from stored procedure
Abby
12/6/2011 4:38 AM
Thanks Dude
Gravatar
# re: Get return value from stored procedure
Hanno
12/7/2011 11:02 PM
Thanx Dude.Realy simple to understand but yet affectf!!
Gravatar
# Thank You very much
babak
12/18/2011 9:27 PM
you help me , I need to know this solution , tnx again .
Gravatar
# re: Get return value from stored procedure
Murali
1/19/2012 6:15 PM
Thanks yar i spend a lots of time for this after seeing your post i found my mistake....
Gravatar
# re: Get return value from stored procedure
Sunny
2/5/2012 9:22 PM
Hi,

Thanks for such a nice post, really helped me. Can you please tell us how to get the multiple return values?
Please assist.

Looking for your sooner reply,
Thanks in Advance !!

Sunny


Post Comment

Title *
Name *
Email
Url
Comment *