Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSDN about stored procedure default return value

Could anyone point exactly where MSDN says thet every user stored procedure returns 0 by default if no error happens? In other words, could I be sure that example code given below when being a stored procedure

IF someStatement
BEGIN
  RETURN 1
END

should always return zero if someStatement is false and no error occurs?

I know that it actually works this way, but I failed to find any explicit statement about this from Microsoft.

like image 687
Ilya Avatar asked Apr 05 '10 13:04

Ilya


People also ask

Does stored procedure have return value?

Stored procedures do not have a return value but can take a list with input, output, and input-output parameters.

What is by default return value in stored procedure SQL Server?

If the return value is not provided, the default is 0. The value is typically used to indicate success or failure of the procedure's execution. The value can be a literal, variable, or an expression that evaluates to an integer value. You can use one or more RETURN statements in a stored procedure.

How many values are returned from stored procedures?

A stored function can return only one value, unlike a stored procedure, which can return multiple values or an entire result set.

Can stored procedures return NULL value?

If you try to return NULL from a stored procedure using the RETURN keyword, you will get a warning, and 0 is returned instead. If a procedure hits an error that requires it to terminate immediately, it will return NULL because it never gets to either the RETURN keyword or the end of the batch!


2 Answers

It looks like once upon a time the return value may have meant something (see reference of pre SQL 2000 BOL return value article) prior to SQL Server 2000. I checked around to see if I could find a listing of these original return codes specifically for MS SQL, and found the following (I don't know its authenticity though, and it doesn't actually list the values).

So, after reading all of these articles it looks like @return_status is a parameter that is ALWAYS returned when a stored procedure is executed (even if you do not use it). According to the RETURN Books online article the return code CANNOT be null.

When used with a stored procedure, RETURN cannot return a null value. If a procedure tries to return a null value (for example, using RETURN @status when @status is NULL), a warning message is generated and a value of 0 is returned.

Running the following T-SQL definitely shows this,

create Procedure Test
as
begin
DECLARE @RTN integer
Return @RTN
END
GO

Exec Test
GO

Drop Procedure Test
GO

You'll receive

The 'Test' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.

In the end it looks like the answer is because @return_status cannot be null (being 0 when not set, or set to NULL)...

As for the error codes mentioned in the supposed BOL article for SQL 7.0, my guess would be an old hold over from Sybase... Sybase 5.0 Manual

Kris

like image 188
KSimons Avatar answered Sep 28 '22 05:09

KSimons


RETURN on MSDN

Edit:

The link says

When used with a stored procedure, RETURN cannot return a null value. If a procedure tries to return a null value (for example, using RETURN @status when @status is NULL), a warning message is generated and a value of 0 is returned.

One could say that no RETURN = RETURN NULL = RETURN 0. But no warning is issued because you have not run RETURN NULL. And zero is expected because it's a stored procedure.

Also, stored proc execution allows for

EXEC @rtn = uspMyProc @p1...

So something must be returned because we always expect a value, never NULL

I've relied on zero being returned for 12 years, even if MSDN doesn't say. As well as many more of us :-)

like image 26
gbn Avatar answered Sep 28 '22 03:09

gbn