4
I need to insert records in a table with data from another continuing the sequence of a column that already exists in it, but the column I want to continue the sequence is not auto-incremented and the table has no primary key.
Situation:
Table suppliers:
cod | nome -------------------- 3 | Aloha 12 | Castor 21 | Tesla
Table contacts:
cod | nome -------------------- 1 | Canada 2 | Asteca 3 | Limiar
I would like to be able to make an Insert more or less like this in the table contacts:
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 being this (repeating the code):
cod | nome -------------------- 1 | Canada 2 | Asteca 3 | Limiar 4 | Aloha 4 | Castor 4 | Tesla
How to solve this problem?
That’s exactly what I needed, thank you very much!
– Laércio Lopes
Hello Rovann, how are you? I know you are an expert in Postgres so I’ll ask you a question, is it possible that Postgres saves a.txt file through a Rigger? For example: after an Insert it saves a column information in txt.
– Laércio Lopes
I did research and found nothing...
– Laércio Lopes
Thanks but this expert title does not fit me =/ rsrs To save some data in a txt file, just use the
copy
example:copy (select 'teste' ) to 'D:/Teste/teste.txt'
whereD:
is a server drive (windows). Just put this command on your Rigger... documentation: https://www.postgresql.org/docs/9.0/sql-copy.html– Rovann Linhalis
Wow, that helped a lot. Thank you.
– Laércio Lopes