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

After update trigger - only insert records not exists Expand / Collapse
Author
Message
Posted Wednesday, July 30, 2014 3:12 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, November 21, 2014 5:22 PM
Points: 1,790, Visits: 3,252
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,'')

)

END





  Post Attachments 
tableScript.txt (0 views, 1.79 KB)
Post #1597999
Posted Wednesday, July 30, 2014 6:44 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 20,815, Visits: 32,748
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,'')

)

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?



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)
Post #1598055
Posted Thursday, July 31, 2014 10:05 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, November 21, 2014 5:22 PM
Points: 1,790, Visits: 3,252
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
Post #1598358
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse