TR_BANKACCOUNTTRANSACTION_DELETE

Definition

Copy


CREATE trigger [dbo].[TR_BANKACCOUNTTRANSACTION_DELETE] ON [dbo].[BANKACCOUNTTRANSACTION] FOR DELETE NOT FOR REPLICATION AS

    BEGIN

        SET NOCOUNT ON                    

        --see if the original bank is closed - if so, raise an error

        if exists(Select DELETED.ID from DELETED INNER JOIN DBO.BANKACCOUNT ON DELETED.BANKACCOUNTID=BANKACCOUNT.ID where
            BANKACCOUNT.STATUSCODE=0) BEGIN
                RAISERROR ('Transactions cannot be deleted from closed bank accounts.',  16, 1)
                ROLLBACK
        END

        --Do not allow to delete posted transaction

        if exists(Select DELETED.ID from DELETED where DELETED.POSTSTATUSCODE = 0)
        begin
            RAISERROR ('ERR_DELETE_POSTED_TRANSACTION.',  16, 1)
            ROLLBACK TRANSACTION
        end

    END