Update trigger that affects multiple rows

  • Hi!

    I need update trigger that could handle an update that affects multiple rows. The problem is that when I am doing an update in table1 the trigger should update table2's values, but this update should be depending on table1's values. So do I have to go through the deleted table and find out row by row what to do in trigger.

  • You're right when you say that you have to 'go through' the deleted table and handle each row. The trigger is only fired once for each query. So if you update ten rows in one statement, the deleted (and inserted) table will contain 10 rows.

    As always, you should try to use a set-based approach in stead of using a cursor to loop through the deleted table.

  • Thanks!

    I think that cursor is only sollution in my problem. Table1 and table2 contains trading info and both tables can have multiple records with same reference.

    So when I update records with specific reference in table1 also table2's records with same reference should be updated, but the update statemens values are depending on table1's original values.

    I manged to create cursor within my trigger but when I am doing updates sql-server gives error message saying that cursor is allready open.

    quote:


    You're right when you say that you have to 'go through' the deleted table and handle each row. The trigger is only fired once for each query. So if you update ten rows in one statement, the deleted (and inserted) table will contain 10 rows.

    As always, you should try to use a set-based approach in stead of using a cursor to loop through the deleted table.


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

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