Solved

SQL Update Trigger

Posted on 2011-02-11
3
307 Views
Last Modified: 2012-05-11
I created a update trigger that will insert the old data (before update) into an Audit table and will also insert the new data (after update) into the Audit table.  I just can't get it to work properly.

ALTER TRIGGER [dbo].[updAuditClientName]
   ON  [dbo].[entity]
   For UPDATE
AS
    if Update (nm_full)
Begin        

      insert into AuditClientName(Log_type,id_Client, client_code, client_nm_full, lastmodified)
            select 'Old', c.id_client, c.client_code, e.nm_full, getDate()
            FROM         dbo.client as c INNER JOIN
                      dbo.entity as e ON c.id_client = e.id
            Join Deleted d on d.id = c.id_client

      insert into AuditClientName(Log_type,id_Client, client_code, client_nm_full, lastmodified)
            select 'New', c.id_client, c.client_code, e.nm_full, getDate()
            FROM         dbo.client as c INNER JOIN
                      dbo.entity as e ON c.id_client = e.id
            Join Inserted i on i.id = c.id_client

End
0
Comment
Question by:vnewman29
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
tlovie earned 500 total points
ID: 34874820
I think that the issue is the you are using e.nm_full - this is the data in the table - it won't be updated when the trigger if firing.  I think you should use this:

ALTER TRIGGER [dbo].[updAuditClientName]
   ON  [dbo].[entity]
   For UPDATE
AS
    if Update (nm_full)
Begin        

      insert into AuditClientName(Log_type,id_Client, client_code, client_nm_full, lastmodified)
            select 'Old', c.id_client, c.client_code, d.nm_full, getDate()
            FROM         dbo.client as c INNER JOIN
                      dbo.entity as e ON c.id_client = e.id
            Join Deleted d on d.id = c.id_client

      insert into AuditClientName(Log_type,id_Client, client_code, client_nm_full, lastmodified)
            select 'New', c.id_client, c.client_code, i.nm_full, getDate()
            FROM         dbo.client as c INNER JOIN
                      dbo.entity as e ON c.id_client = e.id
            Join Inserted i on i.id = c.id_client

End
0
 
LVL 7

Expert Comment

by:tlovie
ID: 34874841
in fact, I don't think you need to join to the entity table at all:

ALTER TRIGGER [dbo].[updAuditClientName]
   ON  [dbo].[entity]
   For UPDATE
AS
    if Update (nm_full)
Begin        

      insert into AuditClientName(Log_type,id_Client, client_code, client_nm_full, lastmodified)
            select 'Old', c.id_client, c.client_code, d.nm_full, getDate()
            FROM         dbo.client as c INNER JOIN
            Deleted d on d.id = c.id_client

      insert into AuditClientName(Log_type,id_Client, client_code, client_nm_full, lastmodified)
            select 'New', c.id_client, c.client_code, i.nm_full, getDate()
            FROM         dbo.client as c INNER JOIN
            Inserted i on i.id = c.id_client

End
0
 

Author Closing Comment

by:vnewman29
ID: 34875057
That worked beautifully.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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