Re: how to monitor table changes
From: AJAY R (ajuonline_at_yahoo.com)
Date: 08/12/03
- Previous message: Kalen Delaney: "Re: select from one db only"
- In reply to: Dejan Sarka: "Re: how to monitor table changes"
- Next in thread: Ray Higdon: "how to monitor table changes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Tue, 12 Aug 2003 11:22:32 +0530
thanks a lot .
"Dejan Sarka" <dejan_please_reply_to_newsgroups.sarka@avtenta.si> wrote in
message news:uhzMkRZXDHA.416@tk2msftngp13.phx.gbl...
> -- 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: Kalen Delaney: "Re: select from one db only"
- In reply to: Dejan Sarka: "Re: how to monitor table changes"
- Next in thread: Ray Higdon: "how to monitor table changes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|