Solved

INSERT multiple rows with a subselect? (PostgreSQL)

Posted on 2002-03-22
3
1,720 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_
3 Comments
 
LVL 22

Accepted Solution

by:
DrSQL earned 200 total points
Comment Utility
 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_
Comment Utility
yep, this is definitely from the "bleeding obvious" department - thanks.
0
 

Expert Comment

by:cyiamxtc
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now