Solved

SQL Update Trigger

Posted on 2011-02-11
3
304 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

919 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

21 Experts available now in Live!

Get 1:1 Help Now