Solved

SQL Update Trigger

Posted on 2010-09-02
10
329 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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now