I'm trying to write some SQL to insert records into a PG table.
This is the logic:
This is the SQL code I tried:
INSERT INTO costestimates (costcat_id, amount, costproject_id, maintenance, position) VALUES (30, 0, costproject.id, false, 22)
(SELECT id FROM costprojects WHERE coststatus_id=1)
I get ERROR: syntax error at or near "("
INTO' creates the destination table, it exclusively owns that table and is quicker compared to the 'INSERT … SELECT'. Because the 'INSERT … SELECT' inserts data into an existing table, it is slower and requires more resources due to the higher number of logical reads and greater transaction log usage.
INSERT INTO SELECT vs SELECT INTO: Both the statements could be used to copy data from one table to another. But INSERT INTO SELECT could be used only if the target table exists whereas SELECT INTO statement could be used even if the target table doesn't exist as it creates the target table if it doesn't exist.
It should be something like this:
INSERT INTO costestimates (costcat_id, amount, costproject_id, maintenance, position)
SELECT 30, 0, id, false, 22 FROM costprojects WHERE coststatus_id=1;
See postgres INSERT syntax
Your syntax is a bit off; what you want is to actually compose the VALUES list using your SELECT statement.
Try something like this:
INSERT INTO
costestimates (costcat_id, amount, costproject_id, maintenance, position)
(SELECT 30, 0, id, false, 22 FROM costprojects WHERE coststatus_id=1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With