How to copy column data from one table to another table

3

I have a table named UC which has an email column, and I have a table backup 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, however I do not know how to do this.

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

update uc 
set uc.email =  backup.email
from uc , backup
where uc.idconsumidor =  backup.idconsumidor;
    
asked by anonymous 23.05.2014 / 18:28

4 answers

2

You can also do with a for:

BEGIN
  FOR rec IN (SELECT bk.email
                    ,uc.id_consumidor
                FROM uc
                    ,backup bk
               WHERE uc.idconsumidor = bk.idconsumidor)
  LOOP
    UPDATE uc
       SET uc.email = rec.email
     WHERE uc.idconsumidor = rec.id_consumidor;
  END LOOP;
END;

In the for query I have already done the join between the tables, so I assure you that it will only bring records that really need to be changed. Inside the loop I do the update.

    
10.03.2015 / 19:02
5
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)

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

    
24.05.2014 / 03:16
4

The script to update the email from the backup table can be done as follows:

/* AMBIENTE DE TESTE: Tabelas temporárias para testar o script */
create global temporary table tmp_uc
(   
    idconsumidor int,
    email varchar2(100)
)
on commit preserve rows;

create global temporary table tmp_backup
(   
    idconsumidor int,
    email varchar2(100)
)
on commit preserve rows;

--Dados para teste
insert into tmp_uc (idconsumidor, email) values (1, 'AAAAA@uc');
insert into tmp_uc (idconsumidor, email) values (2, 'BBBBB@uc');
insert into tmp_uc (idconsumidor, email) values (3, 'CCCCC@uc');
insert into tmp_uc (idconsumidor, email) values (4, 'DDDDD@uc');
insert into tmp_uc (idconsumidor, email) values (5, 'EEEEE@uc');
insert into tmp_uc (idconsumidor, email) values (6, 'FFFFF@uc');
insert into tmp_uc (idconsumidor, email) values (7, 'GGGGG@uc');
insert into tmp_uc (idconsumidor, email) values (8, 'HHHHH@uc');
insert into tmp_uc (idconsumidor, email) values (9, 'IIIII@uc');

insert into tmp_backup (idconsumidor, email) values (1, 'AAAAA@bkp');
insert into tmp_backup (idconsumidor, email) values (2, 'BBBBB@bkp');
insert into tmp_backup (idconsumidor, email) values (3, 'CCCCC@bkp');
insert into tmp_backup (idconsumidor, email) values (4, 'DDDDD@bkp');
insert into tmp_backup (idconsumidor, email) values (5, 'EEEEE@bkp');
insert into tmp_backup (idconsumidor, email) values (6, 'FFFFF@bkp');
insert into tmp_backup (idconsumidor, email) values (7, 'GGGGG@bkp');

/* Atualização dos e-mails na tabela temporária tmp_uc para fins de teste */
update 
(   select
            u.email as email_U,
            bk.email as email_BK
    from tmp_uc u
    inner join tmp_backup bk on u.idconsumidor = bk.idconsumidor
    --where (...)
) t
set t.email_U = t.email_BK

select * from tmp_uc;

commit;

/*
idconsumidor   email
    1          AAAAA@bkp
    2          BBBBB@bkp
    3          CCCCC@bkp
    4          DDDDD@bkp
    5          EEEEE@bkp
    6          FFFFF@bkp
    7          GGGGG@bkp
    8          HHHHH@uc
    9          IIIII@uc
*/

If the script worked the way you want, you can use the following script for your physical tables:

-- Atualização dos dados na tabela física
    update 
    (   select
            u.email as email_U,
            bk.email as email_BK
        from UC u
        inner join backup bk on u.idconsumidor = bk.idconsumidor
        --where (...)
    ) t
    set t.email_U = t.email_BK

    commit;
    
27.05.2014 / 00:34
3

I think the COMMIT was missing at the end:

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

commit;
    
23.05.2014 / 18:55