From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: stan <stanb@panix.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Inserting multiple rows wtih a SELECt in the values clause
Date: Tue, 15 Oct 2019 15:28:40 +0100
Message-ID: <871rveytrt.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <20191015135224.GA3910@panix.com>
References: <20191015135224.GA3910@panix.com>
>>>>> "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)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: andrew@tao11.riddles.org.uk, stanb@panix.com
Subject: Re: Inserting multiple rows wtih a SELECt in the values clause
In-Reply-To: <871rveytrt.fsf@news-spur.riddles.org.uk>
References: <20191015135224.GA3910@panix.com> <871rveytrt.fsf@news-spur.riddles.org.uk>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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