Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


After update trigger - only insert records not exists


After update trigger - only insert records not exists

Author
Message
sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2036 Visits: 3841
I have an address table, and a log table will only record changes in it. So we wrote a after udpate trigger for it. In our case the trigger only need to record historical changes into the log table. so it only needs to be an after update trigger.

I attached the table create script.

The trigger works fine until a day we found out there are same addresses exist in the log table for the same student.

so below is what I modified the trigger to.
I tested, it seems working OK. Also would like to know do I need to use if not exists statement, or just use in the where not exists like what I did in the following code:

ALTER TRIGGER [dbo].[trg_stuPropertyAddressChangeLog] ON [dbo].[stuPropertyAddress]
FOR UPDATE
AS

   DECLARE @rc AS INT ;

SELECT @rc = COUNT(*)
FROM Deleted ;

IF @rc = 0
BEGIN
RETURN ;
END

ELSE
BEGIN

INSERT INTO dbo.stuPropertyAddressChangeLog
( StudentID ,
AddressID ,
ApartmentNum ,
StartDate,
EndDate,
CreateDt ,
CreatedBy
      
)
SELECT del.StudentID ,
del.AddressID ,
del.ApartmentNum ,
COALESCE(del.ChangeDt,del.CreateDt),
SYSDATETIME(),
SYSDATETIME(),
COALESCE(del.ChangedBy,del.CreatedBy)
FROM deleted del
               WHERE NOT EXISTS

         (   SELECT *
               FROM inserted AS ins
               JOIN deleted AS del
               ON ins.studentid=del.studentid AND ins.AddressID=del.addressid
               AND ISNULL(ins.ApartmentNum,'')=ISNULL(del.ApartmentNum,'')
               
         Wink
         
END




Attachments
tableScript.txt (0 views, 1.00 KB)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24167 Visits: 37929
sqlfriends (7/30/2014)
I have an address table, and a log table will only record changes in it. So we wrote a after udpate trigger for it. In our case the trigger only need to record historical changes into the log table. so it only needs to be an after update trigger.

I attached the table create script.

The trigger works fine until a day we found out there are same addresses exist in the log table for the same student.

so below is what I modified the trigger to.
I tested, it seems working OK. Also would like to know do I need to use if not exists statement, or just use in the where not exists like what I did in the following code:

ALTER TRIGGER [dbo].[trg_stuPropertyAddressChangeLog] ON [dbo].[stuPropertyAddress]
FOR UPDATE
AS

   DECLARE @rc AS INT ;

SELECT @rc = COUNT(*)
FROM Deleted ;

IF @rc = 0
BEGIN
RETURN ;
END

ELSE
BEGIN

INSERT INTO dbo.stuPropertyAddressChangeLog
( StudentID ,
AddressID ,
ApartmentNum ,
StartDate,
EndDate,
CreateDt ,
CreatedBy
      
)
SELECT del.StudentID ,
del.AddressID ,
del.ApartmentNum ,
COALESCE(del.ChangeDt,del.CreateDt),
SYSDATETIME(),
SYSDATETIME(),
COALESCE(del.ChangedBy,del.CreatedBy)
FROM deleted del
               WHERE NOT EXISTS

         (   SELECT *
               FROM inserted AS ins
               JOIN deleted AS del
               ON ins.studentid=del.studentid AND ins.AddressID=del.addressid
               AND ISNULL(ins.ApartmentNum,'')=ISNULL(del.ApartmentNum,'')
               
         Wink
         
END





If you don't want dup rows in the log table (dbo.stuPropertyAddressChangeLog), don't you think you should join the deleted table to it not the inserted table in the NOT EXISTS to determine if the data already exists in the log table?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2036 Visits: 3841
Thanks, the same address could be in the log table twice in case they move out an address then after a year move back. so this is correct case.
we just don't want same address shows up for the students in two consecutive records.

The reason I join inserted and deleted is to want to make sure is to prevent some one update the table using the same data multiple times. so in this case inserted and deleted are the same.
then I use not exists to exclude for this case.

Does that sound correct?

Thanks
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