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.

2

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Matt - my mistake was I totally forgot about the 'WHERE EXISTS' part - helped me out!
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);

Comments

0
UPDATE table1 t1
SET id = (SELECT id FROM table2 t2 WHERE t2.name = t1.hw_sn)

that's your homework done for you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.