insert,update and delete trigger

  • I have a table A and another B.I wanted to write a trigger so that if I insert or delete a row in table A it should also insert or delete that row in table B(with id) and also if update a row in table A it should also update the corresponding row in table B. all should be in one trigger.Can anyone help me in this issue.

    Thanks.

  • Why the requirement to put everything in one trigger? Makes life complicated, I think...

    You can write a trigger on all three actions.

    
    
    CREATE TRIGGER tr_InsUpdDel ON tableA
    FOR INSERT, UPDATE, DELETE
    AS
    ...

    Inside the trigger you can check the count of the Inserted and Deleted tables :

    Deleted = 0, inserted <> 0 ==> INSERT

    Deleted = Inserted ==> UPDATE

    Deleted <> 0, inserted = 0 ==> DELETE

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

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