What is the syntax meaning of RAISERROR()

前端 未结 5 2037
情歌与酒
情歌与酒 2021-02-02 05:39

I just created a Instead After Trigger whose syntax is given below:

Create trigger tgrInsteadTrigger on copytableto
Instead of Insert as 
    Declare @store_name         


        
5条回答
  •  醉酒成梦
    2021-02-02 05:51

    The answer posted to this question as an example taken from Microsoft's MSDN is nice however it doesn't directly demonstrate where the error comes from if it doesn't come from the TRY Block. I prefer this example with a very minor update to the RAISERROR Message within the CATCH Block stating that the error is from the CATCH Block. I demonstrate this in the gif as well.

    BEGIN TRY
        /* RAISERROR with severity 11-19 will cause execution
         |  to jump to the CATCH block.
        */
        RAISERROR ('Error raised in TRY block.', -- Message text.
                   5, -- Severity. /* Severity Levels Less Than 11 do not jump to the CATCH block */
                   1 -- State.
                   );
    END TRY
    
    BEGIN CATCH
        DECLARE @ErrorMessage  NVARCHAR(4000);
        DECLARE @ErrorSeverity INT;
        DECLARE @ErrorState    INT;
    
        SELECT 
            @ErrorMessage  = ERROR_MESSAGE(),
            @ErrorSeverity = ERROR_SEVERITY(),
            @ErrorState    = ERROR_STATE();
    
        /* Use RAISERROR inside the CATCH block to return error
         | information about the original error that caused
         | execution to jump to the CATCH block
        */
        RAISERROR ('Caught Error in Catch', --@ErrorMessage, /* Message text */
                   @ErrorSeverity,                           /* Severity     */
                   @ErrorState                               /* State        */
                   );
    END CATCH;
    

提交回复
热议问题