SQL Update one table comparing info from two tables

·

·

,

Example #1 (Using sub query info & exists)

UPDATE T1 A

SET A.OLDNAME = A.NAME, A.NAME = ( SELECT B.NAME FROM T2 B WHERE A.ID = B.ID )

WHERE EXISTS ( SELECT NULL FROM T2 B WHERE A.ID = B.ID )


Example #2 (Using sub query info)

UPDATE T1 A

SET A.OLDNAME = A.NAME,

A.NAME = ( SELECT B.NAME FROM T2 B WHERE A.ID = B.ID )

WHERE T1.AGE >18

 

Example #3 (If you wish to use more explicit join syntax for clarity.)

UPDATE T1

SET T1.OLDNAME=T1.NAME,T1.NAME=T2.NAME

FROM T1

INNER JOIN T2 ON T1.ID=T2.ID;



Leave a Reply

Your email address will not be published. Required fields are marked *