How to update field in one table using after insert trigger..?

  • Hi,

    I have two tables namely account and invoice.

    I want to update one field in account table when there is A NEW RECORD inserted in invoice table. I want to use only triggers nothing else.

    Thanks

  • sanjeev_krs2004 (9/1/2008)


    Hi,

    I have two tables namely account and invoice.

    I want to update one field in account table when there is A NEW RECORD inserted in invoice table. I want to use only triggers nothing else.

    Thanks

    If this is your most optimal way, I cannot tell, but if you insist on using a trigger...

    Create trigger utr_I_Invoice on dbo.invoice

    for insert

    as

    update A

    set yourcol=yourcol + whatever

    from dbo.account A

    inner join inserted I

    on A.account_id = I.Invoice_Account_Id

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Thanks a lot 🙂

    I got it; could you please tell me how to use for loop if I need to pick up one of the different lookup (drop-down) values.

    for example, I want to update the flag to true in account table. but before updating the value I must compare one field (say id) with the one in another table but is in lookup format means you can select one value from the drop down.

    Thanks

  • sanjeev_krs2004 (9/2/2008)


    Thanks a lot 🙂

    I got it; could you please tell me how to use for loop if I need to pick up one of the different lookup (drop-down) values.

    for example, I want to update the flag to true in account table. but before updating the value I must compare one field (say id) with the one in another table but is in lookup format means you can select one value from the drop down.

    Thanks

    euhm ... there is no "dropdown" in sqlserver.

    The only thing you have is a set of rows (from a table, view, ..)

    just add an extra

    and exists (select * from your_lookup_table LT where LT.id = I.id)

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Thanks a lot 😀

    It solved my problem.

    Thanks

Viewing 5 posts - 1 through 4 (of 4 total)

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