Solved

SQL Update Trigger

Posted on 2010-09-02
10
331 Views
Last Modified: 2012-05-10
I have two triggers for an update in the person table.

The first trigger will update the lastmodified date on the update record (below)

ALTER TRIGGER [dbo].[updPerson]
   ON  [dbo].[Person]
   FOR UPDATE
AS

BEGIN
      Update Person
      set Lastmodified = Getdate()
      where PersonID in (select Deleted.PersonID from Deleted)
END

The second trigger will take the old and new record an insert into my Auditperson table (below).

ALTER TRIGGER [dbo].[updAuditPerson]
   ON  [dbo].[Person]
   FOR UPDATE
AS
      if exists (select * from Deleted)
BEGIN
      insert into AuditPerson (PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName,EquitracNumber, SignificantOther, StatusID, LastModifiedBy, LastModified, AuditAction)
      select Deleted.PersonID, Deleted.Prefix, Deleted.FirstName, Deleted.MiddleName, Deleted.LastName, Deleted.Suffix, Deleted.Credentials, Deleted.FullName, Deleted.NickName, Deleted.SAMAccountName, Deleted.EquitracNumber, Deleted.SignificantOther, Deleted.StatusID, SYSTEM_USER, GetDate(), 'Record Updated'
      from Deleted
   
END

Now my auditperson table is showing two records for the same person.

John C. Doe (new record after change), last modified 2010-09-02 14:46:10.460
John Doe (old record before change  - added the C.), last modified 2010-09-02 14:48:20.340
-------------------

Here's the problem my audit table is now showing two records but I want the new record to have the lastest modified date.

I've tried combining this as one trigger and this only added the old record to my audit table.  I need the old and new record but the new record must have the latest modified date.

Any ideas on how I can achieve this in SQL?



0
Comment
Question by:vnewman29
  • 5
  • 5
10 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 33590608
Hi,

How about


ALTER TRIGGER [dbo].[updPerson]
   ON  [dbo].[Person]
   FOR UPDATE
AS BEGIN
      Update Person
        set Lastmodified = Getdate()
          from Person p
             join Deleted d on d.PersonID=d.PersonID

      insert into AuditPerson (PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName,EquitracNumber, SignificantOther, StatusID, LastModifiedBy, LastModified, AuditAction)
select PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName, EquitracNumber, SignificantOther, StatusID, SYSTEM_USER, GetDate(), 'Record Updated'
      from Deleted

      insert into AuditPerson (PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName,EquitracNumber, SignificantOther, StatusID, LastModifiedBy, LastModified, AuditAction)
select PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName, EquitracNumber, SignificantOther, StatusID, SYSTEM_USER, GetDate(), 'Record Updated'
      from Inserted
END


/peter
0
 

Author Comment

by:vnewman29
ID: 33590696
The first part of the trigger updated the last modified field for ALL of the records in the person table.
And the audit table is showing both table except they have the same last modified date which can be fix using the "Waitfor Delay".

Any ideas on how to fix the top part of the trigger.  Should we use the where clause here?
0
 
LVL 22

Expert Comment

by:pivar
ID: 33590739
I'm sorry, first part had a typo, should be

 Update Person
        set Lastmodified = Getdate()
          from Person p
             join Deleted d on d.PersonID=p.PersonID
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:vnewman29
ID: 33590802
I believe I got it to work with your guidance.

ALTER TRIGGER [dbo].[updAuditPerson]
   ON  [dbo].[Person]
   FOR UPDATE
AS BEGIN
      Update Person
        set Lastmodified = Getdate()
          from Person p
          where PersonID in (select Deleted.PersonID from Deleted)
             
      Print 'First Record'
      insert into AuditPerson (PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName,EquitracNumber, SignificantOther, StatusID, LastModifiedBy, LastModified, AuditAction)
select PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName, EquitracNumber, SignificantOther, StatusID, SYSTEM_USER, GetDate(), 'Record Updated'
      from Deleted
      
      Print 'Second Record'
            Waitfor Delay '00:00:01'
      insert into AuditPerson (PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName,EquitracNumber, SignificantOther, StatusID, LastModifiedBy, LastModified, AuditAction)
select PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName, EquitracNumber, SignificantOther, StatusID, SYSTEM_USER, GetDate(), 'Record Updated'
      from Inserted
END

-------------

This is producing what I want.  I am going to test some more.
0
 
LVL 22

Expert Comment

by:pivar
ID: 33590808
I may not have understood the lastmodified requirement. They should have the same time as the modification happens once? Or do you mean the old record should use the modification date for the earlier modification like:

      insert into AuditPerson (PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName,EquitracNumber, SignificantOther, StatusID, LastModifiedBy, LastModified, AuditAction)
select PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName, EquitracNumber, SignificantOther, StatusID, SYSTEM_USER, Lastmodified, 'Record Updated'
      from Deleted


You mustn't use waitfor in the trigger.
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 33590827
If you want the records to differ by a second use:

      insert into AuditPerson (PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName,EquitracNumber, SignificantOther, StatusID, LastModifiedBy, LastModified, AuditAction)
select PersonID, Prefix, FirstName, MiddleName, LastName, Suffix, Credentials, FullName, NickName, SAMAccountName, EquitracNumber, SignificantOther, StatusID, SYSTEM_USER, DATEADD(ss, 1, getdate()), 'Record Updated'
      from Inserted

Not waitfor.
0
 

Author Comment

by:vnewman29
ID: 33590891
Thanks that work perfectly.

0
 

Author Comment

by:vnewman29
ID: 33664393
How do I close this question? I am satsify with the answer.
0
 

Author Comment

by:vnewman29
ID: 34494158
Thanks.
0
 
LVL 22

Expert Comment

by:pivar
ID: 34503522
Hi,

If you're satisfied with my answer, please accept my comment as anwer.

Thanks,

Peter
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question