Solved

show only changed fields in audit generated by SQL Server 2005 After Update trigger

Posted on 2008-11-03
3
301 Views
Last Modified: 2010-04-21
I use the attached trigger to form an audit trail of changes made to records in a project database.  How do I modify the routine to insert only changed values into the audit database and null values for the unchanged fields?
USE [DRDDB]

GO

/****** Object:  Trigger [dbo].[MENYSOWUpdateAudit]    Script Date: 11/03/2008 07:29:51 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

-- =============================================

-- Author:		<Author,,Name>

-- Create date: <Create Date,,>

-- Description:	<Description,,>

-- =============================================

ALTER TRIGGER [dbo].[ProjectsUpdateAudit]

   ON  [dbo].[Projects] 

   AFTER UPDATE

AS 

BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from

	-- interfering with SELECT statements.

	SET NOCOUNT ON;

 

INSERT INTO dbo.ProjectsAudit([idProject]

      ,[Year]

      ,[Client]

      ,[Brand]

      ,[ProjectTitle]

      ,[MediaType]

      ,[MediaTypeDetail]

	,[LastChangedDate]

      ,[LastChangedBy]

	,[ReasonForChange])

SELECT 

[idProject]

	,[Year]

      ,[Client]

      ,[Brand]

      ,[ProjectTitle]

      ,[MediaType]

      ,[MediaTypeDetail]

	,[LastChangedDate]

      ,[LastChangedBy]

	,[ReasonForChange]

FROM DELETED

END

Open in new window

0
Comment
Question by:dvdillonla
3 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
Comment Utility
This will give you the original value when changed (to get the new value switch the aliases):

ALTER TRIGGER [dbo].[ProjectsUpdateAudit]
   ON  [dbo].[Projects]
   AFTER UPDATE
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.

SET NOCOUNT ON;
 
INSERT      dbo.ProjectsAudit([idProject]
            ,[Year]
            ,[Client]
            ,[Brand]
            ,[ProjectTitle]
            ,[MediaType]
            ,[MediaTypeDetail]
            ,[LastChangedDate]
            ,[LastChangedBy]
            ,[ReasonForChange])
SELECT      d.[idProject]
            ,NULLIF(d.[Year], i.[Year])
            ,NULLIF(d.[Client], i.[Client])
            ,NULLIF(d.[Brand], i.[Brand])
            ,NULLIF(d.[ProjectTitle], i.[ProjectTitle])
            ,NULLIF(d.[MediaType], i.[MediaType])
            ,NULLIF(d.[MediaTypeDetail], i.[MediaTypeDetail])
            ,NULLIF(d.[LastChangedDate], i.[LastChangedDate])
            ,NULLIF(d.[LastChangedBy], i.[LastChangedBy])
            ,NULLIF(d.[ReasonForChange], i.[ReasonForChange])
FROM      DELETED d
            Inner Join Inserted i On d.idProject = i.idProject            -- Assuming that idProject is a primary key
END

0
 

Author Closing Comment

by:dvdillonla
Comment Utility
Thank you, more elegant than the multiple case statements I got to work.  Much appreciated!
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

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

9 Experts available now in Live!

Get 1:1 Help Now