Trigger that collects data from two tables

  • Hi,

    Is it possible to create an After Update trigger on table A that can gather both the delete and insert data for the After Update from columns A.1, A.2, A.3 when A.3 is updated to NULL? I need that plus the delete and insert data from columns B.1 and B.2 from table B as well when column A.3 is updated and set to NULL? I have the trigger below that will get me what I need from table A but is it possible to also get the data from table B from this trigger?

    In other words, I'm trying to figure out how to populate the Table B OldWorkFlowStepId and NewWorkFlowStepid columns that are created in the CONTAINER_DEBUG table and declared as variables. The OldWorkFlowStepId value needs to be taken when the delete.CurrentStatusId is taken from the first table and the NewWorkflowStepId value needs to be taken when the insert.CurrentStatusId is taken from the first table.

    How would I go about that? I am trying to troubleshoot a column that is getting set to NULL when it shouldn't and I'm trying to trap exactly where in the workflow this is happening.

    DROP TABLE CONTAINER_DEBUG

    GO

    CREATE TABLE CONTAINER_DEBUG

    ( ContainerIdCHAR(16)

    ,ContainerNameVARCHAR(256)

    ,OldCurrentStatusIdCHAR(16)

    ,NewCurrentStatusIdCHAR(16)

    ,OldLastCompletedTaskIdCHAR(16)

    ,NewLastCompletedTaskIdCHAR(16)

    ,OldWorkflowStepIdCHAR(16)

    ,NewWorkflowStepIdCHAR(16)

    ,LastActivityDateDATETIME )

    GO

    DROP TRIGGER CONTAINER_TRG_BU

    GO

    CREATE TRIGGER CONTAINER_TRG_BU

    ON CONTAINER

    AFTER UPDATE

    AS

    IF ( UPDATE (CurrentStatusId) )

    BEGIN

    --

    DECLARE @OldCurrentStatusIdCHAR(16);

    DECLARE @NewCurrentStatusIdCHAR(16);

    DECLARE @OldLastCompletedTaskIdCHAR(16);

    DECLARE @NewLastCompletedTaskId CHAR(16);

    DECLARE @OldWorkflowStepIdCHAR(16);

    DECLARE @NewWorkflowStepIdCHAR(16);

    --

    SELECT @OldCurrentStatusId

    FROM deleted;

    --

    SELECT @NewCurrentStatusId

    FROM inserted;

    --

    IF ( ISNULL(@OldCurrentStatusId,'XXX') <> 'XXX' AND

    ISNULL(@NewCurrentStatusId,'XXX') = 'XXX' )

    --

    BEGIN

    --

    INSERT INTO CONTAINER_DEBUG ( ContainerId

    ,ContainerName

    ,OldCurrentStatusId

    ,NewCurrentStatusId

    ,LastActivityDate )

    SELECT i.ContainerId

    ,i.ContainerName

    ,d.CurrentStatusId

    ,i.CurrentStatusId

    ,i.LastActivityDate

    FROM Inserted i INNER JOIN Deleted d ON i.ContainerId = d.ContainerId

    --

    RAISERROR ('CurrentStatusId is set to NULL - Aborting...', 16, 10);

    --

    END;

    --

    END;

    GO

  • A table DML trigger only works inside the scope of the table it belongs to. Therefore, you'll need two triggers for the A and B tables respectively. I assume that tables A and B are related in some way, so it should be easy to get the full picture of a particular workflow even with two separate auditing tables.

    Or, create some custom auditing code on the workflow procedure itself.

  • Ok, thanks for the reply! I figured it would only work within itself but wanted confirmation.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply