Results 1 to 5 of 5
  1. #1
    Join Date
    May 2004
    Posts
    105

    Unanswered: Updating a table with another table's contents

    If I have a table with three fields (A, B, C) and 'A' is a primary key, 'B' and 'C' do not have any content and another table with the same number of fields contains the same fields(A, B, C) and the fields do contain data.

    I want to populate the first table with the data from the second table where the primary key 'A' is equal.

    How would the sql work?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    update table1
       set B = t2.B
         , C = t2.C
      from table1 t1
    inner
      join table2 t2
        on t1.A= t2.A
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Dec 2004
    Location
    York, PA
    Posts
    95
    UPDATE tablea
    SET a = tableb.B, C = tableb.C
    FROM tablea , tableb
    WHERE tablea.A = tableb.A
    Sorry to be terse
    some say it's a curse
    I know it's worse
    I'm just diverse

  4. #4
    Join Date
    May 2004
    Posts
    105
    Perfect.

    Thanks so much.

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by White Knight
    UPDATE tablea
    SET a = tableb.B, C = tableb.C
    FROM tablea , tableb
    WHERE tablea.A = tableb.A
    i realize it's just a wee typo, but...

    ... you really don't want to update column a with the contents of tableb.B

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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