Return value from a stored proc on error

前端 未结 5 1640
灰色年华
灰色年华 2020-12-09 09:24

I have an sp in SQL Server that when errors returns -4

What does -4 mean? Is there a table somewhere explaining what the possible return values are?


Th

相关标签:
5条回答
  • 2020-12-09 09:27

    For example

    declare @RetVal int  
    EXEC @RetVal = stpTest  
    select @RetVal
    

    where stpTest is SELECT 1/0 returns -6.

    -6 must mean something!

    If you have an sp that does not return anything i.e. it doesn't have any select statements in it and you do:

    declare @RetVal int  
    EXEC @RetVal = yourSPName  
    

    Then @RetVal will have a value of 0.

    If there is an error then @RetVal will be a value other then zero, for example if the only thing your sp does is SELECT 1/0 then @RetVal will be -6.

    Try it and see


    First of all, thanks for bothering to craft an example that returns -6.

    Here's what the documentation says about -6:

    -6 Miscellaneous user error occurred.

    -6 might be the most amorphous code that SQL Server returns.
    Why? Because the -6 error code is essentially hiding an error deeper in the call stack.

    After troubleshooting this error myself, here are my tips for troubleshooting this error:

    1. If your DAL or application layer generates this error, then run the SQL code in SQL Server Management Studio as the application user, not with your own SQL Server ID. [Why? Because your permissions might not be the same.]
    2. Make the SQL code under inspection as readable as possible. [Why? Because this error may lurk deep within the call stack.]
    3. Failing all else, comment out half of the code. Is the error still occurring? Comment out 50% of the remaining code. Rinse and repeat.
    0 讨论(0)
  • 2020-12-09 09:32

    I think the question should be edited to ask -

    What are the default return values if you dont have a RETURN statement in your stored proc?

    Something I did find was from this link www.redware.com/handbooks/sql_server_handbook/sql_server_stored_procedures.html

    SQL Server will default the return value to zero. The returned values are typically used to return a status flag from the stored procedure with a non-zero value usually indicating failure during processing.

    Returned values are difficult to access using ODBC their use is recommended only to return a success or failure of the stored procedure when communicating with other stored procedures.

    From this link - sqlserverpedia.com/wiki/Stored_Procedures_-_Output_Parameters_&_Return_Values

    The return values -99 through 0 are reserved for SQL Server internal use. You can create your own parameters that can be passed back to the calling program.

    Also another link (I guess) from @Erland Sommarskog www.sommarskog.se/error-handling-I.html

    Return Values from Stored Procedures

    All stored procedures have a return value, determined by the RETURN statement. The RETURN statement takes one optional argument, which should be a numeric value. If you say RETURN without providing a value, the return value is 0 if there is no error during execution. If an error occurs during execution of the procedure, the return value may be 0, or it may be a negative number. The same is true if there is no RETURN statement at all in the procedure: the return value may be a negative number or it may be 0.

    Whether these negative numbers have any meaning, is a bit difficult to tell. It used to be the case, that the return values -1 to -99 were reserved for system-generated return values, and Books Online for earlier versions of SQL Server specified meanings for values -1 to -14. However, Books Online for SQL 2000 is silent on any such reservations, and does not explain what -1 to -14 would mean.

    With some occasional exception, the system stored procedures that Microsoft ships with SQL Server return 0 to indicate success and any non-zero value indicates failure.

    Getting Error Information

    Also if you need to find what the error is (rather than what -6 means) you could try putting your sql into a try catch, ie.

    begin try
    
        select 1/0 as 'an error'
    
    end try
    
    begin catch
    
        select ERROR_NUMBER() as 'ERROR_NUMBER', 
               ERROR_SEVERITY() as 'ERROR_SEVERITY',
               ERROR_STATE() as 'ERROR_STATE',
               LEFT(ERROR_PROCEDURE(),50) as 'ERROR_PROCEDURE',
               ERROR_LINE() as 'ERROR_LINE' , 
               LEFT(ERROR_MESSAGE(),40) as 'ERROR_MESSAGE'    
    end catch 
    
    0 讨论(0)
  • 2020-12-09 09:37

    I'm not sure there's a way to know this without asking the SQL Server devs. It's something lower level than your stored proc evaluating what you're sending it and generating that return code. Unless you've specifically got a RETURN -4 in your code, it's coming from the SQL parser probably.

    0 讨论(0)
  • 2020-12-09 09:41

    If you have a RETURN statement with an explicit return value, that is of course the return value.

    But if there is no RETURN statement, but an error occurs during execution, the return value is 10 minus the severity level of the error. Division by zero is level 16, thus the return value is -6. Permissions errors are typical level 14, thus the return value is -4.

    As you may guess this is not terribly useful, but this: 0 is success, and everything else is an error.

    0 讨论(0)
  • 2020-12-09 09:51

    There is no standard for return codes. You'll have to find out what -4 means in that particular stored procedure. In fact, not all return codes are errors.


    EDIT: counter-example

    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE PROCEDURE [dbo].[RetValTest] 
    AS
    BEGIN
        select 1/0;
    END
    
    GO
    

    Execution:

    DECLARE @return_value int
    
    EXEC    @return_value = [dbo].[RetValTest]
    
    SELECT  'Return Value' = @return_value
    
    GO
    

    Result:

    Msg 8134, Level 16, State 1, Procedure RetValTest, Line 9
    Divide by zero error encountered.
    

    This is with SQL Server 2008.


    Some research suggests that this behavior may be left over from SQL Server 6.0. If that is the case, then you can decide for yourself how reliable it's likely to be, given that they stopped documenting it (and stopped guaranteeing its accuracy) so long ago.


    My "research" is due, with thanks, to SQL Server MVP Tibor Karaszi. His source is Books Online for SQL Server 6.5. Under ""Control-Of-Flow Language", RETURN", he found

    "SQL Server reserves 0 to indicate a successful return and reserves negative values from - 1 through - 99 to indicate different reasons for failure. If no user-defined return value is provided, the SQL Server value is used. User-defined return status values should not conflict with those reserved by SQL Server. The values 0 through -14 are currently in use.
    0 讨论(0)
提交回复
热议问题