交易在触发器中结束。批次已中止

时间:2017-10-25 13:12:43

标签: c# sql sql-server database

获取错误邮件

交易在触发器中结束。批次已中止。

触发代码:

ALTER TRIGGER [dbo].[trgAfterInsertUpdate_DRSDetails] ON [dbo].[TT_DR_Details]
--AFTER INSERT, UPDATE
AFTER UPDATE
AS
BEGIN
-- SET XACT_ABORT ON will cause the transaction to be uncommittable 
SET XACT_ABORT ON

BEGIN TRY
    DECLARE @tempDRDetail_CNote TABLE
    (
        ConsignmentNo   VARCHAR(20),
        Form_type       VARCHAR(5),
        DeliverStatus   VARCHAR(50),
        Reason          VARCHAR(250),
        Remark          VARCHAR(150),
        AreaId          VARCHAR(10),
        EmpId           INT,
        BranchId        INT
    )

    DELETE FROM @tempDRDetail_CNote

    INSERT INTO @tempDRDetail_CNote
    (
        ConsignmentNo
        ,Form_type
        ,DeliverStatus
        ,Reason
        ,Remark
        ,AreaId
        ,EmpId
        ,BranchId
    )
    SELECT
        INS.Cnote
        ,INS.Form_type
        ,INS.DeliverStatus
        ,INS.Reason
        ,INS.Remark
        ,DRM.Area_id
        ,DRM.Emp_Id
        ,DRM.Branch_id
    FROM Inserted INS
    INNER JOIN dbo.TT_DR_Master DRM WITH(READUNCOMMITTED)
    ON INS.Ref_DR_Id = DRM.DR_Id

    UPDATE CA
    SET
        CA.ModifiedOn = GETDATE()
        ,CA.ConsignmentType = INS.Form_type
        ,CA.AlertStatus = 0
        ,CA.ConsignmentStatus = CASE INS.DeliverStatus
                                    WHEN 'Un-Delivered' THEN INS.DeliverStatus + ', Reason: ' + INS.Reason + ', Remark: ' + INS.Remark
                                    WHEN 'Delivered' THEN INS.DeliverStatus --+ ', ' + INS.RecBy
                                    WHEN 'Out For Delivery' THEN INS.DeliverStatus + ', Area Name: ' + ARM.AreaName + ', Delivery Boy: ' + AG.AgentName
                                END
    FROM dbo.SK_Opt_ConsignmentAlert CA WITH(READUNCOMMITTED)
    INNER JOIN @tempDRDetail_CNote INS
    ON CA.ConsignmentNo = INS.ConsignmentNo
    INNER JOIN dbo.TT_AreaMaster ARM WITH(READUNCOMMITTED)
    ON INS.AreaId = ARM.AR_ID AND INS.BranchId = ARM.Branch_ID
    INNER JOIN dbo.AgentMaster AG WITH(READUNCOMMITTED)
    ON INS.EmpId = AG.Agent_ID AND INS.BranchId = AG.BranchId
    WHERE
    ISNULL(CA.IsDMStatus, 0) = 0
END TRY

BEGIN CATCH
 --  IF (@@TRANCOUNT > 0)
    --ROLLBACK TRANSACTION

    -- Test XACT_STATE:  
        -- If 1, the transaction is committable.  
        -- If -1, the transaction is uncommittable and should   
        --     be rolled back.  
        -- XACT_STATE = 0 means that there is no transaction and  
        --     a commit or rollback operation would generate an error.  

    -- Test whether the transaction is uncommittable.  
    IF (XACT_STATE()) = -1  
    BEGIN  
        ROLLBACK TRANSACTION
    END  

    -- Test whether the transaction is committable.  
    IF (XACT_STATE()) = 1  
    BEGIN  
        COMMIT TRANSACTION
    END

    --SELECT CAST(ERROR_NUMBER() AS VARCHAR) + ' - ' + ERROR_MESSAGE() AS ErrorMessage

    INSERT INTO dbo.FK_Opt_ExceptionLog
    (
        APIName
        ,ExceptionMessage
        ,ExceptionTrace
        ,ExceptionMachineIP
        ,CreatedBy
        ,CreatedOn
        ,SourceId
    )
    SELECT
        'trgAfterInsertUpdate_DRSDetails'
        ,CAST(ERROR_NUMBER() AS VARCHAR) + ' - ' + ERROR_MESSAGE()
        ,''
        ,''
        ,'KOLKA-ADMIN'
        ,GETDATE()
        ,5
END CATCH
END

0 个答案:

没有答案