The “right” way to do stored procedure parameter validation

前端 未结 5 1986
灰色年华
灰色年华 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 
        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 
        begin
        set @result = -1
        set @errormessage = 'Condition failed'
        return @result
        end
    

提交回复
热议问题