SQL raiserror没有显示

时间:2013-12-09 23:29:32

标签: sql sql-server transactions

我正在尝试执行一个sproc,但我不确定我是否正朝着正确的方向前进。当我的IF条件为真时,它不会打印我的加速误差......

 set transaction isolation level repeatable read 

 declare @return_value int = 0  
 declare @someValue int = 3
 SET @retry = 3;


 --Keep trying to update 
 --table if this task is 
 --selected as the deadlock 
 --victim.
 WHILE (@retry > 0)
 BEGIN
 BEGIN TRY
    BEGIN TRANSACTION;

    --check someValue
    if @someValue < 5 
        begin
            raiserror ('number is less than 5', 16,1)
            ROLLBACK TRANSACTION
            return 99               
        end

    --all o.k , set retry 0 ending the while, commit transaction--                                                          
    SET @retry = 0;                
    COMMIT TRANSACTION;            
 END TRY
    BEGIN CATCH 
    RAISERROR ('Errors found, please fix these errors and retry. Transaction     Rolled back', 16, 2);
    -- Check error number.
    -- If deadlock victim error,
    -- then reduce retry count
    -- for next update retry. 
    -- If some other error
    -- occurred, then exit
    -- retry WHILE loop.
    IF (ERROR_NUMBER() = 1205)
        SET @retry = @retry - 1;
    ELSE
        SET @retry = -1;  
    IF XACT_STATE() <> 0
        ROLLBACK TRANSACTION;
 END CATCH;
 END; -- End WHILE loop.    

1 个答案:

答案 0 :(得分:0)

catch块将消耗第一个RaIsError。如果你想保留它,也许还要添加其他信息,你可以这样做:

set xact_abort, nocount on;
set transaction isolation level repeatable read; -- Scope is this stored procedure.
declare @LocalTransaction as Bit = case when @@TranCount = 0 then 1 else 0 end;

declare @Return_Value as Int = 0;
declare @SomeValue int = 3;
declare @Retry as Int = 3;

while @Retry > 0
begin
    begin try
        if @LocalTransaction = 1
            begin transaction;

        if @SomeValue < 5
            RaIsError ( 'Number is less than 5.', 16, 1 );

        set @Retry = 0;
        if @LocalTransaction = 1
            commit transaction;
    end try
    begin catch
        if Error_Number() = 1205
            set @Retry -= 1;
        else
        begin
            set @Retry = -1;
            -- Save the exception.
            declare @ErrorLine as Int = Error_Line();
            declare @ErrorMessage as NVarChar(4000) = Error_Message();
            declare @ErrorNumber as Int = Error_Number();
            declare @ErrorProcedure as NVarChar(126) = Error_Procedure();
            declare @ErrorSeverity as Int = Error_Severity();
            declare @ErrorState as Int = Error_State();
            declare @NewLine as Char(2) = Char( 13 ) + Char( 10 ); -- '\r\n'.

            -- Rollback only transactions when there is an active transaction that we started.
            if Xact_State() <> 0 and @LocalTransaction = 1
                rollback transaction;

            -- Exit with the exception.
            RaIsError( '%s%s#%i [Proc: %s/Line %i]', @ErrorSeverity, @ErrorState, @ErrorMessage, @NewLine, @ErrorNumber, @ErrorProcedure, @ErrorLine );
            return 99;
        end;
    end catch;
end;

请注意,错误返回也由catch块处理,因为在RaIsError之后的try块中的代码不应该执行。