Return value from a stored proc on error

前端 未结 5 1659
灰色年华
灰色年华 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.

提交回复
热议问题