SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trigger that collects data from two tables


Trigger that collects data from two tables

Author
Message
pwilly
pwilly
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 101
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



Mansfield
Mansfield
Say Hey Kid
Say Hey Kid (688 reputation)Say Hey Kid (688 reputation)Say Hey Kid (688 reputation)Say Hey Kid (688 reputation)Say Hey Kid (688 reputation)Say Hey Kid (688 reputation)Say Hey Kid (688 reputation)Say Hey Kid (688 reputation)

Group: General Forum Members
Points: 688 Visits: 394
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.
pwilly
pwilly
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 101
Ok, thanks for the reply! I figured it would only work within itself but wanted confirmation.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search