The “right” way to do stored procedure parameter validation

前端 未结 5 1984
灰色年华
灰色年华 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:23

    We normally avoid raiseerror() and return a value that indicates an error, for example a negative number:

    if <errorcondition>
        return -1
    

    Or pass the result in two out parameters:

    create procedure dbo.TestProc
        ....
        @result int output,
        @errormessage varchar(256) output
    as
    set @result = -99
    set @errormessage = null
    ....
    if <errorcondition>
        begin
        set @result = -1
        set @errormessage = 'Condition failed'
        return @result
        end
    
    0 讨论(0)
  • 2021-02-01 03:25

    As you can see from this answer history I followed this question and accepted answer, and then proceeded to 'invent' a solution that was basically the same as your second approach.

    Caffeine is my main source of energy, due to the fact that I spend most of my life half-asleep as I spend far too much time coding; thus I didn't realise my faux-pas until you rightly pointed it out.

    Therefore, for the record, I prefer your second approach: using an SP to raise the current error, and then using a TRY/CATCH around your parameter validation.

    It reduces the need for all the IF/BEGIN/END blocks and therefore reduces the line count as well as puts the focus back on the validation. When reading through the code for the SP it's important to be able to see the tests being performed on the parameters; all the extra syntactic fluff to satisfy the SQL parser just gets in the way, in my opinion.

    0 讨论(0)
  • 2021-02-01 03:27

    I always use parameter @Is_Success bit as OUTPUT. So if I have an error then @Is_success=0. When parent procedure checks that @Is_Success=0 then it rolls back its transaction(with child transactions) and sends error message from @Error_Message to client.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-01 03:33

    I don't think that there is a single "right" way to do this.

    My own preference would be similar to your second example, but with a separate validation step for each parameter and more explicit error messages.

    As you say, it's a bit cumbersome and ugly, but the intent of the code is obvious to anyone reading it, and it gets the job done.

    IF (ISNULL(@fooInt, 0) = 0)
    BEGIN
        RAISERROR('Invalid parameter: @fooInt cannot be NULL or zero', 18, 0)
        RETURN
    END
    
    IF (ISNULL(@fooString, '') = '')
    BEGIN
        RAISERROR('Invalid parameter: @fooString cannot be NULL or empty', 18, 0)
        RETURN
    END
    
    0 讨论(0)
提交回复
热议问题