dbTalk Databases Forums  

RETURN statement odd behavior?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss RETURN statement odd behavior? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jeff Mason
 
Posts: n/a

Default RETURN statement odd behavior? - 07-30-2003 , 09:35 AM






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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.