current row in a trigger

  • I have a simple question (I think). I am writing an update trigger and want to take action based on the value of one of the columns in the (just) updated row. How do I get a pointer to the current row from within a trigger.

  • In the trigger, pull out the Primary Key value from the trigger's 'Inserted' table.

    select (PrimaryKey field) from Inserted

    Or use the same statement to pull out the value of the column you are interested in.

    Note, if your table is likely to be updated in batches, there will be more than one row in the inserted table, therefore pull the select into a cursor and cycle through the values.

    see BOL "inserted tables"

  • In addition to what Smith had said,

    If you are writing a trigger for update statement and if you are updating multiple rows using one statement,

    please remember that you are not updating the primary key (in the update statement).

    SQL server can't identify the same row if the primary key is changed.

    G.R. Preethiviraj Kulasingham

    Chief Technology Officer.

    Softlogic Information Systems Limited,

    14 De Fonseka Place,

    Colombo 05.

    Sri Lanka.

    Cheers,
    Prithiviraj Kulasingham

    http://preethiviraj.blogspot.com/

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

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