Simple Trigger To Move Records

  • I need to create a trigger which moves an entire record from Table1 to Table2, then deletes the record in Table1.

    Table1 Example:

    Col1 = 'abc'    [nvarchar(10)]

    Col2 = 'def'    [nvarchar(10)]

    Col3 = 'ghi'    [nvarchar(10)]

    Col4 = 0        [bit(0)] 

    When Col4 is updated to '1', trigger an insert on Table2.   Table2 has columns identical to Table1.

    After the record is copied, delete the record in Table1.

     

    Thank You So Much!!!!

  • Create trigger tr_name on tableName

    for insert, update

    as

    set nocount on

    insert into table2 (col1, col2, col3, col4) select col1, col2, col3, col4 from inserted where col4 = 1

    delete from table1 where col4=1

    go

  • Excellent answer!!!!  So simple....  

  • Yup, I made it for insert/updates in case you have inserts with that flags on... but this is not necessary if that flag can only be updated.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply