?
Solved

Trigger Audit Help

Posted on 2010-09-08
1
Medium Priority
?
290 Views
Last Modified: 2012-05-10
I have the following I have adapted from a web example, the problem I have is that I need to store both the new and old value for the updates, in additionI also want to add to the audit table, recordid, name etc from the original table. Any help would be good

USE [GeminiSQL_v8]
GO
/****** Object:  Trigger [dbo].[Audit_Trigger]    Script Date: 09/08/2010 16:11:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Audit_Trigger]
ON [dbo].[Cashbook]
FOR UPDATE,DELETE
AS
Declare @OldMessage varchar(200)

IF (SELECT COUNT(*) FROM inserted) > 0
      begin
            set @OldMessage = (SELECT
            (case
            when update(Date)
            then deleted.Date
            end) as Date,
            (case when update(Credit)
            then deleted.Credit
            end) as Credit from deleted
            inner join Cashbook
            on deleted.Reference=Cashbook.Reference
            FOR XML AUTO)
      end
else
      BEGIN
            SET @OldMessage = (SELECT Date ,Credit
            from deleted
            FOR XML AUTO)
end

insert into Audit(Type, TableName, NewValue) values (1, 'Customer', @OldMessage)
0
Comment
Question by:rocky050371
[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
1 Comment
 
LVL 35

Accepted Solution

by:
David Todd earned 2000 total points
ID: 33631698
Hi,

Triggers have two virtual tables inserted and deleted.

That is, for an update, both inserted and deleted tables exist and are populated with old (deleted) and new (inserted) values.

Regards
  David
IF (SELECT COUNT(*) FROM inserted) > 0 
      begin
            set @OldMessage = (SELECT 
            (case
            when update(Date)
            then deleted.Date
            end) as DDate,
            (case when update(Credit)
            then deleted.Credit
            end) as DCredit
		, (case
		when updated( Date )
		then inserted.Date 
		end ) as IDate 
		, (case
		when updated( Credit )
		then inserted.Credit 
		end ) as ICredit 
            from deleted 
		inner join inserted
			on inserted.Reference = deleted.Reference
            inner join Cashbook
            on deleted.Reference=Cashbook.Reference 
            FOR XML AUTO)
      end
else
      BEGIN
            SET @OldMessage = (SELECT Date ,Credit
            from deleted 
            FOR XML AUTO)
end

Open in new window

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

741 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