Solved

SQL Update Trigger

Posted on 2011-02-11
3
308 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
[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
  • 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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

627 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