TSQL: Try-Catch Transaction in Trigger

后端 未结 9 1709
长发绾君心
长发绾君心 2020-12-05 08:09

I am trying to put a try-catch statement inside a trigger using Microsoft Server 2005.

BEGIN TRANSACTION
BEGIN TRY
    --Some More SQL
    COMMIT TRANSACTION         


        
9条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 08:50

    Use SET XACT_ABORT OFF .When Transact-SQL statement encounter error ,it just raised the error message and the transaction continues processing. The following code is to create the trigger:

    Create TRIGGER [dbo].tr_Ins_Table_Master ON [dbo].Table_Master
     AFTER INSERT
    AS
    BEGIN
    set xact_abort off
    BEGIN TRY
            --your SQL          
            INSERT INTO Table_Detail
            SELECT MasterID,Name FROM INSERTED
    
    END TRY
    
    BEGIN CATCH     
        select ERROR_MESSAGE()
    END CATCH
    
    END
    

提交回复
热议问题