Solved

Trigger Audit Help

Posted on 2010-09-08
1
281 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 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 Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Re-appearing SQL Server Agent jobs 7 43
SQL Recursion 6 34
How to keep a record with the highest value 3 43
SQL parsing XML works but want to do it another way 4 23
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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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