?
Solved

SQL Update Trigger

Posted on 2010-09-02
10
Medium Priority
?
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

777 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