The “right” way to do stored procedure parameter validation

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

提交回复
热议问题