Select two tables and insert into two tables at the same time

0

I have four tables

tabela A  aic002
campos :
codigo data nome especie valor rg cpf exped

Tabela B excli
campos
codigo nome rg cpf exped


tabela C imovel
Campos
codigo imovel rua bairro cidade estado 

tabela D  escritura 
codigo  data especie valor  rua bairro cidade estado

I would like to execute a single% w_that%:

  • would get the fields (date species value) FROM A and popularize in D what would be the control chart of scripts
  • get fields (name rg cpf file) from A and popularize it in B thus creating a new customer table
  • pick up (street neighborhood city state) from C and popularize in D to complete customer addresses

How to do this in a single insert knowing that:

Tables A, B, and C are listed by code but the only one that does not repeat the code is Table C's

    
asked by anonymous 02.02.2015 / 23:04

1 answer

1

Joelias,

As you said, it's not possible to do what you want with only INSERT because we have two targets to insert (B, D) but I believe your goal is to avoid using UPDATE in table D. Try the following:

    INSERT INTO D(data, especie, valor, rua, bairro, cidade, estado)        
    SELECT A.data, A.especie, A.valor        
         , C.rua, C.bairro, C.cidade, C.estado        
      FROM A        
      JOIN C ON C.codigo = A.codigo        

    INSERT INTO B(nome, rg, cpf, exped)        
    SELECT A.nome, A.rg, A.cpf, A.exped FROM A        

Please note that I did not insert the column for the codes because it was not clear if the code is PRIMARY KEY (PK) or FOREIGN KEY (FK). From what I understand she's an FK. If it is, you should insert them in the insert as we did with the other columns.

    
05.02.2015 / 17:11