Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update trigger

Status
Not open for further replies.

AccessSQLUser

Programmer
Apr 23, 2001
101
US
Right now I have a trigger that runs each time a record in Table1 is updated. This trigger basically inserts a row into a log table that I have in a separate database. What I would like to do is only insert into that table the values of the fields that were changed. Any ideas?

My current trigger looks like this:
CREATE TRIGGER [trgForUpdate] ON [dbo].[Table1]
FOR UPDATE
AS
Set NoCount On
DECLARE @MyID int
SELECT @myID = ID FROM inserted

Update Table
Set [LastUpdated] = GetDate(), [LastUpdatedBy] = SUSER_SNAME() WHERE ID = @MyID
INSERT INTO DB2.dbo.Table1_Log SELECT Table1.* FROM Table1 WHERE [ID] = @MyID

 

Look in SQL BOL under "Create Trigger." Read about IF UPDATE (column) and IF COLUMNS_UPDATED(). Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Hi,

Here is a pseudocode,

1.0 Declare the number of variables as the number of field in table
2.0 Assign for each variable the value of the field with the deleted table
3.0 for each variable
check if it is the same as in the table inserted
if different
write it in table log

next Kim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top