Почему sql выполняет операторы после появления ошибки? - PullRequest
0 голосов
/ 09 ноября 2018

Я написал этот код. Проблема заключается в том, что после возникновения ошибки он все равно выполняет код, после которого возникает ошибка.

Начните пробовать Начать транзакцию

  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

в блоке catch:

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

почему

1 Ответ

0 голосов
/ 10 ноября 2018
Return 

сделал свое дело. Положив возврат после того, как RAISERROR сделал свое дело.

...