ExecuteNonQuery returns -1

I have a stored procedure and it returns a string value, and I have to execute this SP using the ExecuteNonQuery . When I do this, I get -1 as the return value.

I have never used SET NOCOUNT ON . How can I get the return value? I did not declare the return value as OUTPUT - just SELECT @VARIABLENAME . How can I get the value using ExecuteNonQuery ?

+4
source share
2 answers

ExecuteNonQuery used to execute queries that return only the number of rows affected (although the output parameters can be populated). -1 means no line was affected, or you set nocount.

Use a different API call if you need to read data from sproc.

+7
source

If you only grab one value, I would use the ExecuteScalar command if you are in .Net Langauge. I know Java and the like. have an equivalent.

http://msdn.microsoft.com/en-us/ibrary/system.data.sqlclient.sqlcommand.executescalar.aspx

ExecuteNonQuery can receive returned data if you have output parameters in your SP. This article explains how.

http://msdn.microsoft.com/en-us/library/80x06z3b(v=VS.71).aspx

Although again, if you just return a string, ExecuteScalar is easier to use.

+1
source

All Articles