【问题标题】:Why sql executes the statements after raising the error?为什么sql在引发错误后执行语句?
【发布时间】:2019-04-12 19:56:30
【问题描述】:

我已经写了这段代码。问题是,在引发错误之后,它仍然会在引发错误之后执行代码。

开始尝试 开始交易

  Declare @Days int
  Set @Days= (Select Days from Days where IsActive=1)

  declare @Message varchar(100)
  Set @Message= 'The difference between current date and expiry date must be equal or greater than '+ Convert(varchar,@Days)

  if(datediff(dd, GETDATE(), convert(date,dbo.Func_Base64Decode(@ExpiryDate))) >= @Days)
  Begin
        Set @ErrorMsgType= 0

  End
  Else
  Begin
        Set @ErrorMsgType= 2
        Raiserror(@Message, 16,10);
        return
  End

  //Some insertion code after it.


commit transaction
End Try

在捕获块中:

Begin Catch


            if(@ErrorMsgType = 1)
            Begin
                Raiserror('Opening time cannot be smaller than expiry time', 16,10);
            End
            Else
            Begin
                Set @Message= (Select ERROR_MESSAGE())
                Raiserror(@Message, 16,10);

            End
            RollBack Transaction

    End Catch

为什么?

【问题讨论】:

标签: sql sql-server stored-procedures raiseerror


【解决方案1】:
Return 

成功了。在 RAISERROR 之后放置 return 就可以了。

【讨论】:

    猜你喜欢
    • 2020-01-06
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2023-02-08
    相关资源
    最近更新 更多