Solved

Trigger Audit Help

Posted on 2010-09-08
1
280 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
1 Comment
 
LVL 35

Accepted Solution

by:
David Todd earned 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

828 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