Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iKNoe-0005Qu-Eq for pgsql-general@arkaria.postgresql.org; Tue, 15 Oct 2019 14:28:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iKNod-00034t-4C for pgsql-general@arkaria.postgresql.org; Tue, 15 Oct 2019 14:28:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iKNoc-00034g-Mq for pgsql-general@lists.postgresql.org; Tue, 15 Oct 2019 14:28:46 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iKNoZ-0000TY-ST for pgsql-general@lists.postgresql.org; Tue, 15 Oct 2019 14:28:45 +0000 Received: from [192.168.127.1] (port=31545 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.92.3 (FreeBSD)) (envelope-from ) id 1iKNoX-000KJP-2d; Tue, 15 Oct 2019 14:28:41 +0000 Received: from localhost ([127.0.0.1]:25137 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.92.3 (FreeBSD)) (envelope-from ) id 1iKNoW-00018O-UX; Tue, 15 Oct 2019 14:28:41 +0000 From: Andrew Gierth To: stan Cc: pgsql-general@lists.postgresql.org Subject: Re: Inserting multiple rows wtih a SELECt in the values clause In-Reply-To: <20191015135224.GA3910@panix.com> (stan's message of "Tue, 15 Oct 2019 09:52:24 -0400") Message-ID: <871rveytrt.fsf@news-spur.riddles.org.uk> References: <20191015135224.GA3910@panix.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (berkeley-unix) Date: Tue, 15 Oct 2019 15:28:40 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "stan" == stan 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) where is any valid query returning any number of rows. The use of VALUES for the 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)