Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 11th, 2003, 10:58 AM
Authorized User
 
Join Date: Jun 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trigger

I have the following trigger

CREATE TRIGGER trLogActivity
ON ClassesTest
FOR INSERT, UPDATE, DELETE
AS
insert into TblLogActiveTable (TableName, DeleteOrTruncateDateTime)
Values ('tablename', getdate())

What I want to do is to log the activity of the ClassesTest table. My question is how can I log the table name?

Hope my question is clear to you all.
Thanks.
 
Old September 11th, 2003, 11:16 AM
Authorized User
 
Join Date: Jun 2003
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

You could hard code it. You have created a trigger on ClassesTest table therefore whenever that trigger is actioned then it will always be performed on that table.

e.g.
insert into TblLogActiveTable (TableName, DeleteOrTruncateDateTime)
Values ('ClassesTest', getdate())

Nickie
 
Old September 11th, 2003, 12:16 PM
Authorized User
 
Join Date: Jun 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Additional question please. Can I know what kind of activity happen? Ex: Update or delete or insert have been done to the table that make the trigger fires?
 
Old September 12th, 2003, 03:56 AM
Authorized User
 
Join Date: Jun 2003
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

There are 2 ways I can think of :-
1) Is to have separate triggers for each action e.g.
CREATE TRIGGER trLogActivity
ON ClassesTest
FOR INSERT
AS

CREATE TRIGGER trLogActivity
ON ClassesTest
FOR UPDATE
AS

CREATE TRIGGER trLogActivity
ON ClassesTest
FOR DELETE
AS

Then you know which has been fired and you can record the action.

2) If it is a requirement to have 1 trigger for INSERT, UPDATE and DELETE then you could perform a row count.

e.g. FOR INSERT you can check the inserted table - IF (select count(*) from inserted) > 0........record an INSERT action
FOR UPDATE check for count > 0 in both the inserted and deleted tables
FOR DELETE check for count > 0 in deleted table.

There maybe a better way to do this and possibly somebody else may have something to add to this.

Nickie
 
Old September 12th, 2003, 11:50 AM
Authorized User
 
Join Date: Jun 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I will use your idea for now then. Thanks for your helps.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Trigger Help monika.vasvani SQL Language 2 March 1st, 2007 06:59 AM
Trigger arshad mahmood C++ Programming 4 June 24th, 2004 07:10 AM
Trigger ! minhtri Pro VB Databases 2 June 23rd, 2004 02:27 AM
Trigger arshad mahmood SQL Language 2 May 12th, 2004 05:16 AM
Using instead of trigger dmr999 SQL Server 2000 1 November 29th, 2003 02:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.