Re: how to monitor table changes
From: Dejan Sarka (dejan_please_reply_to_newsgroups.sarka_at_avtenta.si)
Date: 08/08/03
- Previous message: Peter Jakab: "Re: Hotfix 815495 -> Invalid buffer received from client"
- In reply to: AJAY R: "how to monitor table changes"
- Next in thread: AJAY R: "Re: how to monitor table changes"
- Reply: AJAY R: "Re: how to monitor table changes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Fri, 8 Aug 2003 12:03:20 +0200
-- base table
IF OBJECT_ID('dbo.Customer','U') IS NOT NULL
DROP TABLE dbo.Customer
GO
CREATE TABLE dbo.Customer
(id int NOT NULL PRIMARY KEY,
name varchar(30) NULL)
GO
/* Auditing with triggers */
-- log table
IF OBJECT_ID('dbo.CustomerLog','U') IS NOT NULL
DROP TABLE dbo.CustomerLog
GO
CREATE TABLE dbo.CustomerLog
(id int NOT NULL,
name varchar(30) NOT NULL,
loginname sysname, -- login of the actor
username sysname, -- db user of the actor
typeOfEvent char(1), -- type of action - I-nsert, U-pdate, D-elete
deletedInserted char(1), -- does the row come from I-nserted or D-eleted
table
whenEvent datetime DEFAULT GETDATE()) -- timestamp
GO
CREATE TRIGGER logActivity ON dbo.Customer
FOR INSERT, UPDATE, DELETE
AS
IF (SELECT COUNT(*) FROM Inserted) > 0
IF (SELECT COUNT(*) FROM Deleted) > 0
BEGIN
INSERT INTO dbo.CustomerLog
SELECT *, SUSER_SNAME(),USER_NAME(),'U','D', GETDATE()
FROM Deleted
INSERT INTO dbo.CustomerLog
SELECT *, SUSER_SNAME(),USER_NAME(),'U','I', GETDATE()
FROM Inserted
END
ELSE
INSERT INTO dbo.CustomerLog
SELECT *, SUSER_SNAME(),USER_NAME(),'I','I', GETDATE()
FROM Inserted
ELSE
IF (SELECT COUNT(*) FROM Deleted) > 0
INSERT INTO dbo.CustomerLog
SELECT *, SUSER_SNAME(),USER_NAME(),'D','D', GETDATE()
FROM Deleted
GO
-- Dejan Sarka, SQL Server MVP FAQ from Neil & others at: http://www.sqlserverfaq.com Please reply only to the newsgroups. PASS - the definitive, global community for SQL Server professionals - http://www.sqlpass.org "AJAY R" <ajuonline@yahoo.com> wrote in message news:#g9ADaXXDHA.1832@TK2MSFTNGP09.phx.gbl... > Hi , > I want to monitor insert,update,delete on a particular code through trigger > , I am planning to create a another table as a logtable , I can achieve it > for update & delete can anybody give me code to achieve all > insert,update&delete using signle trigger . > > Thanks & Regards > Ajay > >
- Previous message: Peter Jakab: "Re: Hotfix 815495 -> Invalid buffer received from client"
- In reply to: AJAY R: "how to monitor table changes"
- Next in thread: AJAY R: "Re: how to monitor table changes"
- Reply: AJAY R: "Re: how to monitor table changes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|