update related data

  • i have 2 tables with one having some data with first table having id as primary key and auto incremented. secound table will have forign key to the id in 1st table.

    i am using a procedure where i am pass two values one is called @old and other @new.

    i will pass 2 values to my procedure. if i pass proc1 1,3 this means 1 is @old and 3 is @new value and by this the 1st table will populate equal rows with new id as it already has for the column prid which is second and main column for 1st table.

    table1:

    idpridadd1

    11yahoo

    21mark

    32google

    table2:

    idseridflag

    14hello

    15boss

    23yy

    if execute my procedure passing values then :

    proc 1,4

    this means i am passing 1st value for old prid and 2nd one for new prid. so two new rows will be created with some new id since i have 2 rows for prid 1.so the new result of table1 will be :

    table1:

    idpridadd1

    11yahoo

    21mark

    32google

    4 4 xyz

    5 4 abc

    and same will be updated in the table2 but since i have 2 id i.e id 1 and 2 for prid 1 and id 1 in table2 is more then one with some different data so the second parameter in the procedure was 4 which created new id and data in 1st table. i want the id's related to prid 4 to be inserted in table2 but with matching to the old prid i.e 1( as the first parameter in procedure). so here table 2 will have new values inserted as 4 and 5 but since id 1 which is related to the prid 1 of 1st table is repeated more then once so i want the new id 4 to be inserted twice but with serid and flag of id 1 in table2. AS below:

    table2:

    idseridflag

    14hello

    15boss

    23yy

    4 4 hello

    4 5 boss

  • have a look at the OUTPUT clause (new with sql2005) and how it can help you with this request.

    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

  • no, i have completed 90% of this but i want the insertion of data in second table which i think will be done by some loop. but i m not able to achive that . can someone here tell me the last portion of my problem.

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

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