?
Solved

INSERT multiple rows with a subselect? (PostgreSQL)

Posted on 2002-03-22
3
Medium Priority
?
1,773 Views
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:

books
-----
book_id
author
whatever

pages
-----
page_id
book_id (referring to a book from the table above)
description
whatever

notes
-----
page_id
comment
user

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

thanks,
Dmitri
0
Comment
Question by:_D_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Accepted Solution

by:
DrSQL earned 800 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!
0
 

Author Comment

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

Expert Comment

by:cyiamxtc
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);

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 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