trinity
trinity

Reputation: 10494

How to write a DML to update a field in a table based on a field of another table?

Suppose there are two tables 'A' and 'B'. A ( F1 number, F2 number ) and B ( F1 number, F2 number ). I want a dml to do this :-

All rows where A.F1 = B.F1, set A.F2 = B.F2.

(i.e.)

if A : [(1,34)(2,67)] B : [(1,99)(2,100)]

DML should update A to following:

A : [(1,99)(2,100)]

Thanks in advance !

Trinity

Upvotes: 1

Views: 195

Answers (1)

Justin Cave
Justin Cave

Reputation: 231791

It sounds like you just need a correlated update

UPDATE a
   SET f2 = (SELECT f2
               FROM b
              WHERE a.f1 = b.f1)
 WHERE EXISTS (
    SELECT 1
      FROM b
     WHERE a.f1 = b.f1 );

Upvotes: 1

Related Questions