468,242 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

database wide trigger firing with update

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 
  3. GO 
  4. ALTER TRIGGER [dbo].[forumchange] ON test2 
  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
1 1378
2,878 Expert 2GB
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.

Similar topics

6 posts views Thread by Rolf Kemper | last post: by
8 posts views Thread by Remove the obvious for replies | last post: by
2 posts views Thread by Net Virtual Mailing Lists | last post: by
1 post views Thread by aj70000 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.