-2

I have this

Table1   Table2
col1      col1
col2      col2
col3      col3

The table1.col1 is empty right now and it needs to be updated from table2.col1.

but condition should be if the table1.col2 data is matching the data in table2.col2.

Please advice.

Thanks

Dekel
  • 60,707
  • 10
  • 101
  • 129
RobertD
  • 1
  • 1
  • 1
    can you give more details about columns names and values in rows for corresponding columns – Iłya Bursov Aug 10 '17 at 22:27
  • Lets say table. col1 is the employee_key , and this column is empty right now. I have to populate the employee key from table2,col1 as that this is the employee key. But for doing that i need to verify the employee security no which is table1.col2 and also table2.col . so i think i have to make a condition with a join where if both tables columns2 are same then populate the employee keys.. Please advice – RobertD Aug 10 '17 at 22:28
  • so, you want something like `update table1 set col1 = (select col1 from table2 where table2.col2 = table1.col2)`? – Iłya Bursov Aug 10 '17 at 22:29
  • Please read the updated comment and help me out thanks – RobertD Aug 10 '17 at 22:31
  • Please edit your question, don't clarify in comments. Also read & act on [mcve]. Also this is fundamental, read an intro to SQL. Also just googling your title & dbms makes your question unnecessary. – philipxy Aug 11 '17 at 01:35
  • 1
    Possible duplicate of [SQL update from one Table to another based on a ID match](https://stackoverflow.com/questions/224732/sql-update-from-one-table-to-another-based-on-a-id-match) – philipxy Aug 11 '17 at 01:36
  • I am only new to this place. I ll sure do as suggested and will definitely improve on my googling skills . – RobertD Aug 11 '17 at 16:48
  • I hope with the passage of time my sql skill along with searching skills both improve and i dont be that much of a haassle. – RobertD Aug 11 '17 at 16:49

1 Answers1

0

For SQL Server, you can use this:

Merge
    Table1 as TARGET
Using
    Table2 as SOURCE
On
    TARGET.col2 = SOURCE.col2
When MATCHED
    Then Update Set TARGET.col1 = SOURCE.col1
Leo.W
  • 539
  • 1
  • 7
  • 18
  • 1
    You don't even know the dbms, comment for clarification. Except this is a very basic faq, google for a duplicate, don't answer. – philipxy Aug 11 '17 at 01:38