I need to insert records into one table with data from another by continuing the sequence of a column that already exists in it, but the column I want to continue the sequence is not self-incrementing and the table does not have a primary key.
Situation:
Suppliers table:
cod | nome -------------------- 3 | Aloha 12 | Castor 21 | Tesla
Contacts table:
cod | nome -------------------- 1 | Canada 2 | Asteca 3 | Limiar
I would like to be able to insert something like this in the contacts table:
INSERT INTO contatos (cod, nome)
SELECT (SELECT MAX(cod) FROM contatos)+1, nome FROM fornecedores;
Expecting this result:
cod | nome -------------------- 1 | Canada 2 | Asteca 3 | Limiar 4 | Aloha 5 | Castor 6 | Tesla
But the result is this (repeating the code):
cod | nome -------------------- 1 | Canada 2 | Asteca 3 | Limiar 4 | Aloha 4 | Castor 4 | Tesla
How do I solve this problem?