Update table

  • I have two tables which are identical - apart from the names of the tables themselves.
     
     
    How do I update table 1 with the data form table 2 based (joined on an ID field)
     
    When I try to construch an update query in SQL it makes me choose one table - where I want the corresponding value in table 2 so need to have both table in the query?
     
    Thanks
     
  • An update statement as given below should be ok. check and let me know.

     

    update test1

    set test1.col2 = test2.col2,

    test1.col3 = test2.col3

    from test1,test2

    where test1.col1 = test2.col1

    Cheers,
    Sugeshkumar Rajendran
    SQL Server MVP
    http://sugeshkr.blogspot.com

  • Yes thats perfect! Its annoying the query builder didn't get me to there.

     

     

    THanks

  • Any chance that table 2 could have new rows in it that aren't in table 1, yet?

    Heh... using a tool like Query Builder is great for certain tasks... but, I equate it to someone having an awesome sports car and then letting their Grandmother drive them around in it Although she knows where all the super-markets are and she knows how to cook (you'll be well fed), you're not going to get anywhere fast with the performance capable by what's under the hood, you won't improve your driving skills, and you won't ever get to some of the "cool" places 'cause she just doesn't know where they're at ...

    --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 4 posts - 1 through 3 (of 3 total)

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