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 2000 Forums
 SQL Server Development (2000)
 Source of Trigger

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-10-24 : 09:43:19
Clyde writes "I have a trigger on a table called problem
that fires when the status of a record changes.

The trigger inserts the information into my AuditTrail table

In the problem table I have 2 fields problemNumber and RecordNumber. I need to include one of these fields in my auditTrail table so I can identify the record the change occurred on. I am not sure how this is done. Please help or point me in the right direction.

Thank you for reading and taking the time to help.

CREATE TRIGGER AuditUpdate ON Problem
FOR INSERT,UPDATE
AS
DECLARE @comments VarChar(255)


IF UPDATE(Status)
SELECT @comments='Status Column was modified'


INSERT INTO AuditTrail(TableName, ActionTaken,
ActionUser, ActionDate, Comment) VALUES
('Problem','U',User_Name(),GetDate(),
@comments)"

nr
SQLTeam MVY

12543 Posts

Posted - 2005-10-24 : 13:32:22
Note that IF UPDATE(Status) doesn't mean that the value was changed - only that the field was included in an update statement.
You either need to change the structure of the audit table to include the other fields or include the values in the comments

INSERT INTO AuditTrail(TableName, ActionTaken,
ActionUser, ActionDate, Comment) select
'Problem','U',User_Name(),GetDate(),
@comments + '<problemNumber=' + convert(varchar(20),problemNumber) + '><RecordNumber=' + convert(varchar(20),RecordNumber) + '>'
from inserted

have a look at
http://www.mindsdoor.net/#Triggers
especially the audit trails


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -