Update a third table based on comapring two other tables.

  • I have 3 tables

    Table A - 5 fields, Table B - 10 Fields, Table C - 5 Fields

    Table A and C have the same data structure.  Table A,B and C have one field (columnX) in common (guid) type.  I want to update table C with data from table A only if columnX data in table A = table B columnX data.

    If TableA c1 = TableB c1 then update TableC all fields with records from TableA

    I tried using a subquery but I just couldn't get it to work. I'm sure someone has done this a miilions times please enlighten me.

    Thanks

  • Did you try this :

    update X column from table C where X in (select column X from table A inner join B on A.X = B.X)

    This will not work if the subquery returns more than 1 value and this is the way it should be because if the subquery returns more than 1 value, SQL server can't decide what value it wants to update the X column.

     

  • update c

    set c.column1 = a.column1,  

         c.column2 = a.column2,  

         c.column3 = a.column3,

         c.column4 = a.column4    

    from Table_C c

    join Table_A a on c.columnx = a.columnx

    join Table_B b on b.columnx = a.columnx

     

     

     

     

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

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