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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now