为什么不提起Raiserror?

时间:2018-10-13 10:01:25

标签: sql tsql stored-procedures sql-server-2012 subquery

如果我在交易中包含此区块:

Begin Try
  Begin Transaction tran


    Declare @ExpiryDate varchar(100)
        Set @ExpiryDate= (Select NewExpiryDate from NIT n where n.NITNo= @NitNo)
        IF(CONVERT(DATE,@ExpiryDate)) < convert(date,getdate())
        Begin
                RAISERROR('error occurred', 16,1);
                return;
        End

  Commit Transaction tran
End Try
Begin Catch
                select ERROR_MESSAGE()

                Rollback transaction tran1
End Catch

...它到达catch块,但是永远不会引发错误。它永远不会抛出错误。为什么?

1 个答案:

答案 0 :(得分:0)

我将其重写为:

Begin Try
  Begin Transaction tran;

  Declare @ExpiryDate DATE =(Select NewExpiryDate from NIT where n.NITNo= @NitNo);

  IF @ExpiryDate < convert(date,getdate())
      RAISERROR('error occurred', 16,1);

  Commit Transaction tran;
End Try
Begin Catch
   IF @@TRANCOUNT > 0 Rollback transaction tran1;
   THROW;  -- rethrowing error
End Catch