SQL RAISERROR未显示

SQL RAISERROR未显示,sql,sql-server,transactions,Sql,Sql Server,Transactions,我正在尝试执行一个存储过程,但我不确定我是否朝着正确的方向前进。当我的IF条件为真时,它将不会打印我的raiseerrors 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 --s

我正在尝试执行一个存储过程,但我不确定我是否朝着正确的方向前进。当我的IF条件为真时,它将不会打印我的raiseerrors

 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.    
设置事务隔离级别可重复读取
声明@return\u value int=0
声明@someValue int=3
设置@retry=3;
--继续尝试更新
--表中显示此任务是否已完成
--被选为死锁
--受害者。
而(@retry>0)
开始
开始尝试
开始交易;
--检查某个值
如果@someValue<5
开始
raiserror('数字小于5',16,1)
回滚事务
返回99
结束
--一切正常,设置重试0结束时,提交事务--
设置@retry=0;
提交事务;
结束尝试
开始捕捉
RAISERROR('发现错误,请修复这些错误并重试。事务回滚',16,2);
--检查错误号。
--如果死锁受害者出错,
--然后减少重试次数
--对于下一次更新,请重试。
--如果有其他错误
--发生,然后退出
--在循环时重试。
如果(错误号()=1205)
设置@retry=@retry-1;
其他的
设置@retry=-1;
如果XACT_STATE()0
回滚事务;
末端捕捉;
完;——边结束边循环。

第一个
RaIsError
将被catch块消耗。如果要保留它,或者添加其他信息,可以执行以下操作:

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;
设置xact\u中止,不计数打开;
设置事务隔离级别可重复读取;--作用域是这个存储过程。
当@TranCount=0时,将@LocalTransaction声明为Bit=case,然后将1或0结束;
将@Return_值声明为Int=0;
声明@SomeValue int=3;
将@Retry声明为Int=3;
当@Retry>0时
开始
开始尝试
如果@LocalTransaction=1
开始交易;
如果@SomeValue<5
RaIsError('数字小于5',16,1);
设置@Retry=0;
如果@LocalTransaction=1
提交事务;
结束尝试
开始捕捉
如果错误_Number()=1205
设置@Retry-=1;
其他的
开始
设置@Retry=-1;
--保存异常。
将@ErrorLine声明为Int=Error_Line();
将@ErrorMessage声明为NVarChar(4000)=错误消息();
将@ErrorNumber声明为Int=Error_Number();
将@ErrorProcedure声明为NVarChar(126)=Error_Procedure();
将@ErrorSeverity声明为Int=Error_Severity();
将@ErrorState声明为Int=Error_State();
将@NewLine声明为Char(2)=Char(13)+Char(10);--'\r\n'。
--仅当存在我们启动的活动事务时回滚事务。
如果Xact_State()为0且@LocalTransaction=1
回滚事务;
--异常退出。
RaIsError(“%s%s#%i[Proc:%s/行%i]”、@ErrorSeverity、@ErrorState、@ErrorMessage、@NewLine、@ErrorNumber、@ErrorProcedure、@ErrorLine);
返回99;
结束;
末端捕捉;
结束;
请注意,错误返回也由catch块处理,因为RaIsError之后的try块中的代码不应执行