sql – Copying Column Data from One Table to Another Table

Question:

I have a table called UC that has an email column, and I have a backup table of it.

I need to copy the data (emails) from the email column of the backup table to the email column of the uc table, but I don't know how to do this.

I made several attempts, without success. I had never encountered this situation, copying data between tables.

update uc 
set uc.email =  backup.email
from uc , backup
where uc.idconsumidor =  backup.idconsumidor;

Answer:

UPDATE uc u
   SET (email) = (SELECT b.email
                  FROM backup b
                  WHERE u.idconsumidor = b.idconsumidor)
WHERE EXISTS (
    SELECT 1
      FROM backup b2
     WHERE u.idconsumidor = b2.idconsumidor)

The outside WHERE (the 2nd) exists to prevent you from updating records in table u that do not have matches in table backup . Without this where, unmatched records would have the email column set to NULL.

Scroll to Top