Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trigger that collects data from two tables Expand / Collapse
Author
Message
Posted Friday, April 12, 2013 3:35 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, July 2, 2013 3:14 PM
Points: 35, Visits: 100
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
( ContainerId CHAR(16)
,ContainerName VARCHAR(256)
,OldCurrentStatusId CHAR(16)
,NewCurrentStatusId CHAR(16)
,OldLastCompletedTaskId CHAR(16)
,NewLastCompletedTaskId CHAR(16)
,OldWorkflowStepId CHAR(16)
,NewWorkflowStepId CHAR(16)
,LastActivityDate DATETIME )
GO

DROP TRIGGER CONTAINER_TRG_BU
GO

CREATE TRIGGER CONTAINER_TRG_BU
ON CONTAINER
AFTER UPDATE
AS
IF ( UPDATE (CurrentStatusId) )
BEGIN
--
DECLARE @OldCurrentStatusId CHAR(16);
DECLARE @NewCurrentStatusId CHAR(16);
DECLARE @OldLastCompletedTaskId CHAR(16);
DECLARE @NewLastCompletedTaskId CHAR(16);
DECLARE @OldWorkflowStepId CHAR(16);
DECLARE @NewWorkflowStepId CHAR(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


Post #1441944
Posted Wednesday, April 17, 2013 9:56 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 2:20 PM
Points: 364, Visits: 384
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.
Post #1443589
Posted Thursday, April 18, 2013 10:49 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, July 2, 2013 3:14 PM
Points: 35, Visits: 100
Ok, thanks for the reply! I figured it would only work within itself but wanted confirmation.
Post #1444004
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse