Dear friends,
I want to know does stored procedure in sql server always returns a value?
pls reply
thanks
aamir
Does storedProcedure always Returns a value
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 2, 2011, 11:09 PM EDT
If you use ExecuteReader, the Return values are stores as rows and columns. You have to loop through to reader to fetch data.
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.
SqlConnection connection = new SqlConnection(connectionString);
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(); // Return value will be stored in returnValue
connection.Close();
-----------------------------------------------------------------------------------------
If this post helps you, then mark as "Correct Answer"
Thank You