Reputation: 5953
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 "("
Upvotes: 16
Views: 40005
Reputation: 181
Insert Into Table1
Select id,name,age from Table2;
Table1
is our newly created table where we are dumping records from Table2
of specific columns only. We don't need to mention Table1
columns name if we are taking same columns in Select
query.
Upvotes: 0
Reputation: 1468
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;
Upvotes: 33
Reputation: 5107
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)
Upvotes: 2