I have a stored procedure simplified to the following code:
SET NOCOUNT ON
DECLARE @count INT
DECLARE @temp TABLE
INSERT INTO @temp
SELECT @count = count(*) FROM @temp
DECLARE [cursor] FOR SELECT id FROM @temp
OPEN [cursor] FETCH NEXT FROM [cursor] INTO @id WHILE @@fetch_status = 0
BEGIN
exec anoterStoredProc @id
FETCH NEXT FROM [cursor] INTO @id
END
CLOSE [cursor]
DEALLOCATE [cursor]
SET NOCOUNT OFF
RETURN @count
But ExecuteNonQuery()
still returns -1!
I can use select @count
and ExecuteScalar()
but want understand why my code doesn't work.
Seems that NOCOUNT OFF
doesn't reset the server behavior, and NOCOUNT ON
is still in-place.
First, don't mess with SET ROWCOUNT - that does something very dangerous (if used incorrectly).
Return values are actually handled by adding a parameter with "direction" of ReturnValue. Alternatively, SELECT the value, and use ExecuteScalar.