By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,842 Members | 2,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,842 IT Pros & Developers. It's quick & easy.

database wide trigger firing with update

P: 1
hi all,

i want to do something like that; if someone tries to update a table, the trigger will fire and add a new record that contains the field name, old value and new value.

i have already done a small part of it.

the code is below.

Expand|Select|Wrap|Line Numbers
  1. set ANSI_NULLS ON 
  2. set QUOTED_IDENTIFIER ON 
  3. GO 
  4. ALTER TRIGGER [dbo].[forumchange] ON test2 
  5. FOR UPDATE 
  6. AS 
  7. BEGIN 
  8. Declare @newNAME varchar(64) 
  9. Declare @oldNAME varchar(64) 
  10. declare @date datetime 
  11. Select @oldNAME = ad from deleted 
  12. Select @newNAME = ad from inserted 
  13. select @date = getdate() 
  14. if (@newNAME is not null) and (@oldNAME is not null) 
  15. insert into test3 (oldNAME,newNAME,date) 
  16. values (@oldname,@newname,@date) 
  17. END
as you see, the trigger fires up with any update on test2 db.

but current code only controls the "name" field of the table name_surname.
i want it to go after every field and log them.

thank you.
Oct 22 '08 #1
Share this Question
Share on Google+
1 Reply


ck9663
Expert 2.5K+
P: 2,878
Use UPDATE() to see if the column was updated. Then do the necessary update.

Happy coding!

-- CK
Oct 22 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.