TSQL Create trigger with transaction and catch try block

I have some questions about a transaction in a trigger for which I have not yet found the answer.

CREATE TRIGGER A_AI ON A
AFTER INSERT AS

    BEGIN TRY --is the try block 1 transaction ? or do I have to begin the transaction?

        --BEGIN TRAN: may I start the transaction like this?
        -- SOME DANGEROUS OPERATIONS

        COMMIT --has this sense here?
    END TRY
    BEGIN CATCH --silent catch no rasing errors.
        ROLLBACK TRANSACTION -- does it work without BEGIN TRANSACTION? if so, is it the whole try block?
    END CATCH

GO
+3
source share
1 answer

A trigger is always executed in the context of a transaction — each DML statement works in a transaction. This is usually hidden from the fact that Implicit Transactions is set to commit automatically in SQL Server.

If you roll back from a trigger, it will be (as always with a rollback) rollback of all transactions nested or not.

, ( ) .

, , . , SQL SQL-, , .

+5

Source: https://habr.com/ru/post/1752044/


All Articles