Insert into with select [closed]

0

I need to do an insert of type

INSERT INTO your_table SELECT * FROM temp_table;

But, I need the id, and the second column that is an id_fk I can set.

I need to do a transfer of the tuple to another user, copying all information but passing new id, f_id and name

Anything wrong?

    
asked by anonymous 01.11.2017 / 19:31

2 answers

1

Try to do this as follows:

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 the 'INSERT INTO' and 'SELECT' fields should be the same. Note 2: Since you did not specify in which database you are doing this SQL (Mysql, Postgree, SQlServer), I used the PostgreSQL syntax, maybe you need to adapt the code to the database you are using

    
01.11.2017 / 19:52
0

you 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;
    
01.11.2017 / 19:36