TSQL: Try-Catch Transaction in Trigger

后端 未结 9 1679
长发绾君心
长发绾君心 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:39

    In my experience any error caught in a try catch in a trigger will rollback the entire transaction; you may be able to use a save transaction. I think you need to look at whats happening in "Some more sql" and determine if you can write case / if statements around it to stop the error.

    What you may be able todo depending on what you are doing is use a save transaction and capture that in the catch

    In your code something like this

    SAVE TRANSACTION BeforeUpdate;
    BEGIN TRY
            --Some More SQL
    END TRY
    BEGIN CATCH
    ROLLBACK TRANSACTION BeforeUpdate;
            return
    END CATCH
    

提交回复
热议问题