The “right” way to do stored procedure parameter validation

前端 未结 5 2048
灰色年华
灰色年华 2021-02-01 02:45

I have a stored procedure that does some parameter validation and should fail and stop execution if the parameter is not valid.

My first approach for error checking look

5条回答
  •  我在风中等你
    2021-02-01 03:32

    I prefer to return out as soon an possible, and see not point to having everything return out from the same point at the end of the procedure. I picked up this habit doing assembly, years ago. Also, I always return a value:

    RETURN 10
    

    The application will display a fatal error on positive numbers, and will display the user warning message on negative values.

    We always pass back an OUTPUT parameter with the text of the error message.

    example:

    IF ~error~
    BEGIN
        --if it is possible to be within a transaction, so any error logging is not ROLLBACK later
        IF XACT_STATE()!=0
        BEGIN
            ROLLBACK
        END
    
        SET @OutputErrMsg='your message here!!'
        INSERT INTO ErrorLog (....) VALUES (.... @OutputErrMsg)
        RETURN 10
    
    END
    

提交回复
热议问题