pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedInserting multiple rows wtih a SELECt in the values clause
2+ messages / 2 participants
[nested] [flat]
* Inserting multiple rows wtih a SELECt in the values clause
@ 2019-10-15 13:52 stan <stanb@panix.com>
0 siblings, 1 reply; 2+ messages in thread
From: stan @ 2019-10-15 13:52 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
OK, now that figured out how to return the constant, this is the final
query I need to run.
INSERT into rate
(
employee_key ,
project_key ,
work_type_key ,
rate
)
VALUES
(
(
SELECT
employee.employee_key ,
project.project_key ,
work_type.work_type_key ,
1 as rate
FROM employee
CROSS JOIN project
CROSS JOIN work_type
)
);
But this fails.
I suspect this may be because the SELECT in the values clause returns
multiple rows?
--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Inserting multiple rows wtih a SELECt in the values clause
@ 2019-10-15 14:28 Andrew Gierth <andrew@tao11.riddles.org.uk>
parent: stan <stanb@panix.com>
0 siblings, 0 replies; 2+ messages in thread
From: Andrew Gierth @ 2019-10-15 14:28 UTC (permalink / raw)
To: stan <stanb@panix.com>; +Cc: pgsql-general@lists.postgresql.org
>>>>> "stan" == stan <stanb@panix.com> writes:
stan> I suspect this may be because the SELECT in the values clause
stan> returns multiple rows?
Understand this: VALUES is really just a special form of SELECT that
returns only the specific rows that you tell it to construct. Every
single row returned by a VALUES clause is separately constructed.
i.e. VALUES (...),(...); will return exactly two rows regardless of
what is inside the (...). VALUES (...); is always exactly one row.
And so on.
The general form of INSERT is actually:
INSERT INTO table(columns) <query>
where <query> is any valid query returning any number of rows. The use
of VALUES for the <query> is just a convenient shorthand for cases where
the exact number of rows to be inserted, and their content, is known in
advance.
So, if you're inserting some set of rows generated from a query, the
word VALUES should not appear in the top-level statement. What you want
is:
INSERT INTO rate(employee_key, project_key, work_type_key, rate)
SELECT employee.employee_key,
project.project_key,
work_type.work_type_key,
1 as rate
FROM employee
CROSS JOIN project
CROSS JOIN work_type;
--
Andrew (irc:RhodiumToad)
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2019-10-15 14:28 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-10-15 13:52 Inserting multiple rows wtih a SELECt in the values clause stan <stanb@panix.com>
2019-10-15 14:28 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox