Update by cursor

  • Hi All,

    Can anyone help me?

    I want update my table by using a cursor. I have two tables and want to update table A by using data from table B.Both the tables have same columns but I want to update Text2 for TableA with the Text2 of TableB.

    TableA.BookID = TableB.BookID

    Update

    TableA.Text2  Should be  TableB.Text2

    Is there any other method to do so?

    Please let me know.

    Thanks

    Zia

     

  • I'm not sure why you have data duplicated in two separate tables... that usually constitutes a violation of 3rd Normal Form... but it's your database...

    ... something like this will do the job (certainly don't need a cursor, either)...

     UPDATE ta

        SET Text2 = tb.Text2

       FROM TableA ta

      INNER JOIN TableB tb

         ON ta.BookID = tb.BookID

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks Jeff,

    The reason for the duplicated data in two tables coz by mistake I have updated that column while removing opostify from it.So I restore the specific data from the backup and it was the only data sitting duplicate in my database.I now have dropped the results duplicated data.

    Again thank you very much.

    Zia

  • hi

    if the tables have same columns then you should be able to write a single update query.

    update TableB set TableB.Text2 = TableA.Text2 FROM TableA where TableA.BookID = TableB.BookID.

    "Keep Trying"

  • Looks pretty familiar, huh?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • You're welcome, Zia.  And thank you for taking the time to explain your duplicates.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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