1

I got two tables: table1 and table2

table1:

HW_SN (varchar2)         ID (number)

123ERZ123                (empty)
124NIJD11
4125ERT22
....

-

table2:

ID (number)             Name (varchar)

85442                   123ERZ123
85471                   124NIJD11
12478                   4125ERT22
...                     ...

table1 has another column called ID which is empty.

I need to check table1-HW_SN if its the same to table2-Name - if yes it should add table2-ID to table1-ID.

Matt
  • 14,906
  • 27
  • 99
  • 149
piguy
  • 516
  • 3
  • 10
  • 30

3 Answers3

1
UPDATE table1
SET ID = (SELECT table2.ID
                     FROM table2
                     WHERE table1.HW_SN = table2.Name)
WHERE EXISTS (SELECT table2.ID
                     FROM table2
                     WHERE table1.HW_SN = table2.Name);
Matt
  • 14,906
  • 27
  • 99
  • 149
  • Thanks Matt - my mistake was I totally forgot about the 'WHERE EXISTS' part - helped me out! – piguy Apr 21 '15 at 11:12
0

This is an update operation. Unfortunately, Oracle does not support join with update, so you have two choices. You can use merge. Or, in this case, a subqueries accomplish what you want:

update table1
    set id = (select id from table2 t2 where t2.name = table1.hw_sn and rownum = 1)
    where exists (select 1 from table2 t2 where t2.name = table1.hw_sn);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0
UPDATE table1 t1
SET id = (SELECT id FROM table2 t2 WHERE t2.name = t1.hw_sn)

that's your homework done for you!

davegreen100
  • 2,055
  • 3
  • 13
  • 24