471,888 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,888 software developers and data experts.

RETURN statement odd behavior?

Consider the following (questionable, to be sure, but syntactally legal) stored
procedure (using the Northwind database):

Create Procedure Test
As
Return (Select Count(*) From Orders)
Select * From Orders;

If you execute this SP in QA via:

Declare @test int;
exec @test = test;
print 'Order count = ' + cast(@test as varchar);

you'll notice that you get a correct return value, but you also get the entire
resultset from the Orders table(!), this despite the fact that BOL states:

"The RETURN statement unconditionally terminates a query, stored procedure, or batch.
None of the statements in a stored procedure or batch following the RETURN statement
are executed."

If you change the procedure definition to:

Create Procedure Test
As
Declare @tmp int;
Select @tmp = Count(*) From Orders;
Return @tmp;

Select * From Orders;

Then things behave as expected, returning only the return value.

Worse, if the procedure is defined as:

Create Procedure Test
As
Return (Select Count(*) From Orders);
Return -1;

This will return a -1 regardless of how many rows are in the Orders table.

Anybody know what's going on here?

- Jeff
Jul 20 '05 #1
0 1610

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.