The Transaction Ended In The Trigger The Batch Has Been Aborted

前端 未结 2 530
后悔当初
后悔当初 2021-01-12 10:42

I am using Sql Server 2008. I have a Trigger which updates my two other tables. I have read the Stack over flow this link enter link description he

相关标签:
2条回答
  • 2021-01-12 11:29

    You can save the error details in a Error Log table, so you can come back later to investigate.
    Something like

    ALTER TRIGGER [Inventory].[StockUpdationOnIssue]
    ON              [Inventory].[StockIssueDetails]
    AFTER           INSERT  
    AS
    BEGIN
        BEGIN TRY
            BEGIN TRAN
                        INSERT INTO TableA 
                            (col1, col2,col3
                            )
                        SELECT      I.col1,I.col2,si.col3
                        FROM        inserted I
                        INNER JOIN  Inventory.StockIssue SI
                        ON          SI.StockIssueId = I.StockIssueId
    
                        INSERT INTO TableB
                            (col1, col2,col3
                            )
                        SELECT      I.col1,I.col2,si.col3
                        FROM        inserted I
                        INNER JOIN  Inventory.StockIssue SI
                        ON          SI.StockIssueId = I.StockIssueId
    
            COMMIT TRAN
        END TRY
        BEGIN CATCH
            DECLARE @ErrorMsg VARCHAR(MAX), @ErrorNumber INT, @ErrorProc sysname, @ErrorLine INT 
    
            SELECT @ErrorMsg = ERROR_MESSAGE(), @ErrorNumber = ERROR_NUMBER(), @ErrorProc = ERROR_PROCEDURE(), @ErrorLine = ERROR_LINE();
            RollBack Tran;
    
            INSERT INTO ErrorLog (ErrorMsg,  ErrorNumber,  ErrorProc,  ErrorLine)
            VALUES               (@ErrorMsg, @ErrorNumber, @ErrorProc, @ErrorLine)
        END CATCH
    END
    

    Being table ErrorLog like:

    CREATE TABLE ErrorLog 
    (
       ErrorLogID INT IDENTITY(1,1),
       ErrorDate DATETIME DEFAULT (GETUTCDATE()),
       ErrorMsg VARCHAR(MAX), 
       ErrorNumber INT, 
       ErrorProc sysname, 
       ErrorLine INT 
    )
    
    0 讨论(0)
  • 2021-01-12 11:33

    I got the same error message. You don't need transaction within the trigger as it has a transaction by default; i.e. you don't need begin tran nor commit tran. But you could use in the catch the rollback tran and it will rollback in case of exceptions.

    ALTER TRIGGER [Inventory].[StockUpdationOnIssue]
    ON              [Inventory].[StockIssueDetails]
    AFTER           INSERT  
    AS
    BEGIN
        BEGIN TRY
                        INSERT INTO TableA 
                            (col1, col2,col3
                            )
                        SELECT      I.col1,I.col2,si.col3
                        FROM        inserted I
                        INNER JOIN  Inventory.StockIssue SI
                        ON          SI.StockIssueId = I.StockIssueId
    
                        INSERT INTO TableB
                            (col1, col2,col3
                            )
                        SELECT      I.col1,I.col2,si.col3
                        FROM        inserted I
                        INNER JOIN  Inventory.StockIssue SI
                        ON          SI.StockIssueId = I.StockIssueId
        END TRY
        BEGIN CATCH
            RollBack Tran;
        END CATCH
    END
    
    0 讨论(0)
提交回复
热议问题