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)
 Audit trigger

Author  Topic 

lastnewbie
Starting Member

1 Post

Posted - 2006-06-28 : 04:23:30
Hello,

I have coded one trigger FOR INSERT, UPDATE, DELETE. I want to know which action (insert, delete or insert) has caused the execution of the trigger. Is there any property that offers that.

I do not want to proceed by comparing the rows number nor by compparing the content of the fields.

Thank you for your help.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-06-28 : 04:43:41
Is this a Home Work Assignment?

DECLARE	@Ins INT, @Del INT

SELECT @Ins = COUNT(*) FROM inserted
SELECT @Del = COUNT(*) FROM deleted

IF @Ins > 0 AND @Del > 0
PRINT 'UPDATE action'

IF @Ins > 0 AND @Del = 0
PRINT 'INSERT action'

IF @Ins = 0 AND @Del > 0
PRINT 'DELETE action'

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2006-06-28 : 04:47:01
Hi,
if you have specified he trigger FOR INSERT, UPDATE, DELETE
then it will be executed whenever an Insert or an update or a delete statement is executed on that table. However, you can check
If Update(column) to check whether an statement update statement has fired the trigger execution
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-06-28 : 05:20:47
"If Update(column) to check whether an statement update statement has fired the trigger execution"

Inserts will do that too, won't they?

If you are trying to build a trigger to copy changed data to an Audit table have look at:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210&SearchTerms=Triggers+to+audit+changes

and in particular

INSERT dbo.MyAuditTable
SELECT [AuditType] = CASE WHEN I.MyPK IS NULL THEN 'D' ELSE 'U' END,
[AuditDate] = GetDate(),
D.*
FROM deleted D
LEFT OUTER JOIN inserted I
ON I.MyPK = D.MyPK

Kristen
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-06-28 : 05:31:10
See also these links for audit triggers:

http://www.nigelrivett.net/SQLTriggers/Triggers_2_Creating_Audit_Trails.html
http://www.nigelrivett.net/SQLTriggers/AuditTrailTrigger.html


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page
   

- Advertisement -