How to create this trigger

  • I have 2 table

    registrants and print_audit. In these 2 tables I have columns setup to handle the dates

    What I need it to create a trigger that will look at the registrants table and if the print date changes, copy the Key and old value of the print date to the second table.

    Nutshell version

    Table 1 and table 2

    create trigger on table 1 to look at print_date column

    IF print_date column changes

    copy key and current value in table 1 to table 2

    Please advise.

  • If I understand you correctly, it will look something like this:

    create trigger Table1_PrintDate on dbo.Table1

    on update

    as

    insert into dbo.Table2 (KeyValue, Print_Date)

    select i.KeyValue, i.Print_Date

    from inserted i

    inner join deleted d

    on i.KeyValue = d.KeyValue

    and i.Print_Date != d.Print_Date;

    If you take a look at creating triggers in Books Online, it will explain how the "inserted" and "deleted" tables work. What I did here was join the two so that it will find any where the KeyValue is the same, but the Print_Date is different, which will be all the ones where there was a change in that column.

    I'm not sure if you wanted to insert into Table2, or update it, so I assumed insert. Should be easy enough to modify into an update, if that's what you really need.

    Does that help? Do you understand what it's doing?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

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

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