Need help in writting a Trigger...

  • Hi,

    I am new SQL Server. Here is the situation for writting a trigger. If any one share the sample code trigger, that will be great.

    I have set of 5 tables. And another set of 5 trigger tables.

    I need to write trigger whenever a new record is inserted. The trigger then update/insert the same data into the trigger tables.

    I know it is very simple but, there is no much time to read and write the code.

    I really appreciate your co-operation in this regard.

     

    Thanks.

    Srinivas

  • Well, you have left quite a bit of information out.

    you have 5 tables, and each table has a matching table?

    I'm assuming yes.

    So for each of the 5 tables you need 2 triggers.

    a for insert, and a for update

    Create trigger MyTrigger

    on Table1

    AFTER INSERT

    As

    Insert into Table1Audit(col1, col2, col3...)

    Select col1,col2,col3...

    from Inserted

    GO

    Create trigger MyTrigger

    on Table1

    AFTER Update, Delete

    As

    Insert into Table1Audit(col1, col2, col3...)

    Select col1,col2,col3...

    from Deleted

    GO

     

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

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