Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a return value of 0 always a success in stored procedures?

If a stored procedure returns a value of zero, does that always mean it was run successfully? I am using MS SQL Server 2008.

like image 378
chobo Avatar asked May 17 '11 17:05

chobo


People also ask

Is it mandatory to return value in stored procedure?

The RETURN statement is used to unconditionally and immediately terminate an SQL procedure by returning the flow of control to the caller of the stored procedure. It is mandatory that when the RETURN statement is executed that it return an integer value. If the return value is not provided, the default is 0.

What does return 0 do in SQL Server?

Return Value in SQL Server Stored Procedure The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.

How many values can be returned from a stored procedure?

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

No, you can return something yourself

example

CREATE PROC pr_test AS 
SELECT 1/0

RETURN 0
GO

Now run it

DECLARE @i INT
exec @i = pr_test

SELECT @i  -- will be 0

DROP PROC pr_test

Now let's do it again without the return statement

CREATE PROC pr_test2 AS 
SELECT 1/0

GO

DECLARE @i INT
exec @i = pr_test2

SELECT @i  -- will be - 6

Better to use an output parameter to pass back statuses and or messages

like image 193
SQLMenace Avatar answered Nov 16 '22 01:11

SQLMenace


An @@ERROR return value of "zero" indicates that your procedure completed without any errors.

Of course, that doesn't mean that it did what you wanted it to...

Can you be more specific about what you're looking at?

like image 43
Seattle Badger Avatar answered Nov 16 '22 00:11

Seattle Badger