Insert into with select

Asked

Viewed 4,406 times

0

I need to do an insertion of the type

INSERT INTO your_table SELECT * FROM temp_table;

but, I need the id, and the second column which is an id_fk I can set.

I need to transfer the tuple to another user, copying all information but, passing new id, id_fk and name

some trick?

  • Explain better, your question is not clear

  • It would be interesting to add the structure of the tables involved.

  • If possible set some example, because it was not very clear the doubt

2 answers

1

try to do it this way:

INSERT INTO tabela (nome_campo1,nome_campo2) VALUES (select nome_campo1,nome_campo2 from tabela)

Note: Just replace field name with the fields in question, remembering that both after 'INSERT INTO' and 'SELECT', the order of the fields should be the same.

Note 2: Since you did not specify in which database you are making this SQL (Mysql, Postgree, Sqlserver), I used the Postgresql syntax, maybe you need to adapt the code for the database q vc is using

0

just change the select, and do not use *

INSERT INTO your_table 
              SELECT 
                 '1' as novo_id, 
                 '2' as nova_fk, 
                 t.outros_campos 
              FROM temp_table t;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.