Solved

How do I use a SELECT with an INSERT statment?

Posted on 2007-12-05
4
161 Views
Last Modified: 2010-03-20
I wish to insert a series of rows into a table. The rows are obtained as a result of an intersection between two other tables. For example:

INSERT INTO A values (b_id,'xxx','yyy',...)
...
SELECT b_ id
FROM b
WHERE b_bill_date = to_date('04-DEC-2007','DD-MM-YYYY')
INTERSECT
SELECT DISTINCT c_id
FROM c, d
WHERE c_term_code = '200720'
AND d_rsts_code = d_code
AND d_incl_sect_enrl = 'Y';

What is the correct syntax for this query?
0
Comment
Question by:geeta_m9
4 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 20413075
INSERT INTO TableA (column1, column2, column3)
SELECT x, y, z
FROM <one or more joined tables>
0
 

Author Comment

by:geeta_m9
ID: 20413091
Only the values in column A will be obtained from the result of the intersection. The columns in the other values are going to be fixed, i.e. hardcoded in.
0
 
LVL 3

Expert Comment

by:randomjames
ID: 20413553
For hard-coded values in fields instead of a field name just put the value in single quotes for text or date and just put the number you need if it's a number.
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20416639
Try this. As the other expert has mentioned, you can use constants in selects
INSERT INTO A values (b_id,f1,f2,...)

SELECT b_ id, 'xxx','yyy',....

FROM b

WHERE b_bill_date = to_date('04-DEC-2007','DD-MM-YYYY')

INTERSECT

SELECT DISTINCT c_id

FROM c, d

WHERE c_term_code = '200720'

AND d_rsts_code = d_code

AND d_incl_sect_enrl = 'Y';

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

23 Experts available now in Live!

Get 1:1 Help Now