Results 1 to 5 of 5

Thread: Problem with Update Trigger

  1. #1
    Join Date
    Aug 2004
    Posts
    52

    Problem with Update Trigger

    Hi All,
    I am facing problem for writting trigger for update.
    I've 2 tables. Say Table A & Table B.
    Whenever I update any field of any record in Table A, trigger should update the same data for corresponding record in Table B.
    As in this case there is no fix field which will get updated so how to write trigger when u don't know which field getting updated? Is there any way to replace the updated rows as it is in another table ?
    Request you to help me out.

    Thanks in advance.
    Avadhoot

  2. #2
    Join Date
    Feb 2003
    Posts
    1,048
    Triggers are placed on tables, not fields. You don't need to know which field is getting updated.

    Can you clarify your second question? Are you wanting to replace the updated row with a row from a different table instead of what the update created? If so, then use an "Instead of trigger".

    If you want to update a row in a different table with the row that was updated in the target table, then a regular trigger for update will do the trick. You'll need the trigger to to update the second table with the value in the "inserted" virtual table.

  3. #3
    Join Date
    Aug 2004
    Posts
    52

    Re: Problem in Update Trigger

    Hi,
    Thanks for the reply.
    Case is something like this

    table A (emp_no, empname, email)
    table B (empno, name, email_id)

    I've just taken some sample flds here in above example.
    So what happens, table A & table B contains same data. So I wrote trigger for insert successfully.
    Now problem is with Update.
    I am trying to construct a trigger which is useful for me in Update phase.
    Say, if any row in table A gets updated, subsequent changes should also happen for that record in table B. ( But I am unable to do it because field names in another table are slightly different). In case of insert, I can make use of 'Inserted' table but in case of updation unable to proceed ahead.
    I am trying to construct trigger in MS-SQL Server2000.
    Kindly guide me how should I proceed ahead in the same.

    Thanks again.

    Avadhoot

  4. #4
    Join Date
    Feb 2003
    Posts
    1,048
    You can make use of the "inserted" table for the update trigger as well. An update is actually a delete and insert. So for an update, the "deleted" virtual table will have the record as it was before the update, and the "inserted" virtual table will have the record as it is after the update.

  5. #5
    Join Date
    Aug 2004
    Posts
    52
    Hi,
    thanks alot.
    It worked out using Inserted and Deleted tables as you said

    Avadhoot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •