Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Audit Log

Author  Topic 

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-02-28 : 10:25:28
Hi guys

i have to put a audit log on a table. either of the operation update or delete i need the data (old values) to be copied to a different table.
the option i have a put trigger or do we have any other efficent way to log the information.

thanks

========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone

mfemenel
Professor Frink

1421 Posts

Posted - 2009-02-28 : 10:34:26
Unless you're running 2008 the trigger and table approach is your best approach.

Mike
"oh, that monkey is going to pay"
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-02-28 : 10:35:17
sorry i missed out
i am using SQL 2005

========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-28 : 19:18:11
Like this:

CREATE TRIGGER Trg_name ON table
AFTER INSERT,UPDATE,DELETE
AS
BEGIN

IF EXISTS (SELECT * FROM INSERTED)
AND NOT EXISTS(SELECT * FROM DELETED)
BEGIN
INSERT INTO table2
SELECT your fields
FROM INSERTED i
END

IF EXISTS (SELECT * FROM INSERTED)
AND EXISTS(SELECT * FROM DELETED)
BEGIN
UPDATE t
SET t.field1=i.field1,t.field2=i.field2,...
FROM table2 t
INNER JOIN INSERTED i
ON i.FKCol=t.PKCol
END

IF NOT EXISTS (SELECT * FROM INSERTED)
AND EXISTS(SELECT * FROM DELETED)
BEGIN
DELETE t
FROM table2 t
INNER JOIN DELETED d
ON d.FKCol=t.PKCol
END

END
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-03-02 : 03:11:33
Thanks sodeep

I need to place a log audit for many tables (master) in this case can i have a one common code for all these to log. my table structures will be different i can do a comma sperated data and store in one field.

please suggest me..

========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone
Go to Top of Page
   

- Advertisement -