Solved

Trigger Audit Help

Posted on 2010-09-08
1
273 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

16 Experts available now in Live!

Get 1:1 Help Now