INSERT multiple rows with a subselect? (PostgreSQL)

Posted on 2002-03-22
Last Modified: 2010-08-05
Ok, I am not sure if it works like this, but it's worth a try.

I have three tables, a hypothetical example with the same relationships would be:


book_id (referring to a book from the table above)


what I need, is the quickest way, given a book_id a user name and a comment to add a row to the notes table for each page associated with that book_id, with the same comment and user (I know it doesn't make much sense in this example, just trust me, it's what I need)

to help visualize it (let's say we are doing this for book 3, the comment is "good book" and the user name is 'billg'):I

NSERT INTO notes VALUES ((SELECT page_id FROM books WHERE book_id = 3),'good book', 'billg')

now this obviously doesn't work, but if it did, it would create as many rows in notes as there are pages with book_id = 3  and each one would have the same user and comment.

Is there a way to do this in one statement?  (I am hopeful, but doubtful)

Oh, as the title says, I am using postgres

Question by:_D_
LVL 22

Accepted Solution

DrSQL earned 200 total points
ID: 6888721
 D  ,
   You want an insert select:

insert into notes (page_id,comment,user)
  select pageid,'good book','billg' from pages
    where book_id=3;

Good luck!

Author Comment

ID: 6889151
yep, this is definitely from the "bleeding obvious" department - thanks.

Expert Comment

ID: 15072864
You can also do it like this as well....(Oracle 8i):

insert into CR_TNW
(select * from CR_TNWDD);

this is selecting across a link too:

insert into CR_TNW
(select * from PPD.CR_TNW@TRMSP);


Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
understanding output of mysql version 2 60
SQL 2014 AG in-place upgrade to SQL 2016 2 75
Cassandra Select Query 1 73
ORA-12560: TNS:protocol adapter error 8 128
In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question