views:

1214

answers:

4

I have a stored procedure and if the stored procedure does this:

SELECT 0 As Ret
DELETE FROM table where value1 = 1

Returns 1 row result with its value of 0 and column name Ret

But if I do this:

DELETE FROM table where value1 = 1
SELECT 0 As Ret

I get no returned results.

My question is, how do I get the second variation to return a value.

I'm using C++ and ODBC.

+1  A: 

Explicitly set a variable to @@RowCount and then return it

Declare @ret int
Select @ret = @@RowCount
Select @ret

EDIT

It's returning 0 for the second form because the row count for the select of 0 as ret is 0 rows. So you'll want to store the number of rows returned immediately after your Delete.

cmsjr
I don't think you understood the question. It's returning no rows in the second case. The problem is having the delete first and then the select. It never returns to me a result set.
Brian R. Bondy
I actually want it to return 0 in the second case. (Hence the SELECT 0 As Ret) But the problem is that it's not returning anything.
Brian R. Bondy
I'm sorry, I thought you were concerned with manipulating the @@RowCount value. Both of the variants you give return results for me (with different @@Rowcount values accessible after execution). Perhaps you could share more of the code where you are not receiving a value.
cmsjr
I don't know if this is relevant to your situation, but I do recall having had issues with some connectivity methods where the result set was not accessible until after the count of records affected was accessed.
cmsjr
+3  A: 

See the setting for - SET NOCOUNT.

shahkalpesh
+1 though short, it does point the direction towards the correct answer
Sam Saffron
+1  A: 

shahkalpesh is right, nocount should work for this, though I would recommend not using a result set as a return value and instead using the RETURN statement, and interrogating the return value of the proc.

SET NOCOUNT ON 
DELETE FROM table where value1 = 1
SET NOCOUNT OFF
SELECT 0 As Ret
Sam Saffron
I think the problem may be that I eventually wanted to change my code to: DELETE FROM table whre value1 = 1SELECT @@ROWCOUNT
Brian R. Bondy
Would SET NOCOUNT ON stop @@ROWCOUNT from being filled?
Brian R. Bondy
nope .. @@ROWCOUNT will always get populated, it has some edge cases around usage with tables that have triggers, there are other options for that
Sam Saffron
A: 

Ok I found that you can use the ODBC call SQLMoreResults to get the next result set. So you can keep calling this SQLMoreResults function until there are no more result sets left.

In my case after calling SQLMoreResults I got my expected result set.

This is pretty cool because it means that a single stored procedure can return multiple result sets. I never knew that it could.

@Sambo99 and @shakalpesch your suggestions also works and returns only 1 result.

Brian R. Bondy