database wide trigger firing with update

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • overlordqd
    New Member
    • Oct 2008
    • 1

    #1

    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.

    Code:
    set ANSI_NULLS ON 
    set QUOTED_IDENTIFIER ON 
    GO 
    ALTER TRIGGER [dbo].[forumchange] ON test2 
    FOR UPDATE 
    AS 
    BEGIN 
    Declare @newNAME varchar(64) 
    Declare @oldNAME varchar(64) 
    declare @date datetime 
    Select @oldNAME = ad from deleted 
    Select @newNAME = ad from inserted 
    select @date = getdate() 
    if (@newNAME is not null) and (@oldNAME is not null) 
    insert into test3 (oldNAME,newNAME,date) 
    values (@oldname,@newname,@date) 
    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.
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Use UPDATE() to see if the column was updated. Then do the necessary update.

    Happy coding!

    -- CK

    Comment

    Working...