Create a trigger that inserts values into a new table when a column is updated

95,099

Solution 1

Something like this should do what you need. You would have the INSERT statements below insert values indicating the operation performed into MyLogTable.

CREATE TRIGGER [dbo].[TRIG_MyTable]
ON [dbo].[MyTable]
AFTER INSERT, UPDATE

AS 

DECLARE @INS int, @DEL int

SELECT @INS = COUNT(*) FROM INSERTED
SELECT @DEL = COUNT(*) FROM DELETED

IF @INS > 0 AND @DEL > 0 
BEGIN

    -- a record got updated, so log accordingly.

    INSERT INTO MyLogTable
    SELECT 'New Values', getdate() FROM INSERTED

    INSERT INTO MyLogTable
    SELECT 'Old Values', getdate() FROM DELETED

END

ELSE 
BEGIN

    -- a new record was inserted.

    INSERT INTO MyLogTable
    SELECT 'Insert', getdate() FROM INSERTED

END

If you wanted to you could also add columns from INSERTED and DELETED to your log table as well if you wanted to capture the actual column values that got inserted or updated.

Solution 2

This is for all changes and all columns, but you can modify how you like:

USE [DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[trMyTrigger]
ON [dbo].[MyTable]
AFTER INSERT, UPDATE, DELETE
NOT FOR REPLICATION
AS
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with caller queries select statements.
    -- if an update/insert/delete occurs on the main table, the number of records affected
    -- should only be based on that table and not what records the triggers may/may not
    -- select.
SET NOCOUNT ON;

    -- Determine if this is an insert,update, or delete action

    DECLARE @action AS CHAR(1)
    DECLARE @count AS INT
    SET @action = 'I' -- SET action to 'I'NSERT by default.
    SELECT @count = count(*) FROM DELETED
    IF @count > 0
        BEGIN
            SET @action= 'D' -- SET action to 'D'ELETED.
            SELECT @count = count(*) FROM INSERTED
            IF @count > 0
                SET @action = 'U' -- SET action to 'U'PDATED.
        END

    IF @action = 'D'
        -- THIS IS A DELETE RECORD ACTION
        BEGIN
            INSERT INTO myBackupTable
        SELECT *,GETDATE() AS changeDate, 'DELETE' AS task FROM DELETED
        END
    ELSE
        BEGIN
            IF @action = 'I'
                 -- this is an INSERT record action
                BEGIN
                    INSERT INTO myBackupTable
                    SELECT *,GETDATE() AS changeDate, 'INSERT' as task FROM INSERTED
                END
             ELSE
                -- this is an UPDATE record action
                BEGIN
                    INSERT INTO myBackupTable
                    SELECT *,GETDATE() AS changeDate, 'UPDATE' as task  FROM INSERTED
                END
        END

Solution 3

create trigger trigge on abs
instead of update as

declare @idd int , @pricee money
  select @idd= ProductID from inserted 
  select @pricee = ListPrice from inserted 
  insert into prod values ( @idd , @pricee)
  print ' cannot change'
Share:
95,099
Standage
Author by

Standage

An educated fool with money on my mind!

Updated on March 01, 2020

Comments

  • Standage
    Standage about 4 years

    I've been looking at some previous answers on triggers on here but can't find what I need exactly but I'm sure my question has been asked/answered before.

    I'm trying to keep track of any changes to columnA and columnB in table1.

    If this value changes I want to keep track of the values by inserting the existing value and the new Value into a different table with a date.

    I've been looking at using something like this for the insert but not sure how to add get the existing and new values of the source table (table1):

    CREATE TRIGGER NewTrigger ON table1
    FOR INSERT
    AS
    
    INSERT INTO table2
            (columnA , columnB, todaysDate)
        .
        .
    
    go
    

    I need to use (I think) the

    Before update ON table1 FOR EACH ROW
       .
       .
       .
    BEGIN
    

    and look through all the changes and insert these first then do the same after the Update?

  • Standage
    Standage about 12 years
    ok cheers for the answer I'll take a look at this and come back to you.
  • Standage
    Standage about 12 years
    Sorry I did want to keepn track of the actual values in the colummns that got inserted or Updated, how easily is that done?
  • Darth Continent
    Darth Continent about 12 years
    Woohoo! :) Regarding your last question, if you haven't already found a way, basically you'd just add any additional columns to the INSERT statements for insert or update, so that anytime the table is modified whatever columns you define will be updated with the before and after values.
  • SpringLearner
    SpringLearner about 9 years
    I am not good in sql so asking sily question.Is this answer captures from all the tables present in DB or we have to give a particular table name?
  • jimdrang
    jimdrang about 9 years
    This is for one table. You would have to write your own dynamic script to make the trigger work on multiple/all tables.