1

I'm working through a problem where I need to insert 135 rows into a recently created table with a select statement. I have a handful of NOT NULL constraints on that table and I don't understand how to alter my SELECT to insert the correct information.

Here's what I'm trying to do:

CREATE SEQUENCE target_table_s1 START WITH 1001;

INSERT INTO target_table(colA,ColB,ColC,ColD,ColE)
target_table_s1.NEXTVAL,
(SELECT (colB,colC,ColD)
FROM source_table),
colE;

Where colA is a sequence number (to provide a primary key for the target_table) and colE basically just needs to be something simple like SYSDATE.

Any suggestions on how I can make this work? I know that what I've written above isn't going to work but it's the best way I can illustrate what I'm trying to accomplish. Do I need to find a way to put my sequence inside the select statement so it follows the proper "INSERT INTO SELECT" format?

0

1 Answer 1

4

I think you should just be using the INSERT INTO ... SELECT construct here:

INSERT INTO target_table (colA,ColB,ColC,ColD,ColE)
SELECT target_table_s1.NEXTVAL, ColB, ColC, ColD, SYSDATE
FROM source_table

I assume above that you want to insert the SYSDATE into column E.

Sign up to request clarification or add additional context in comments.

5 Comments

So this is pretty much what I'm looking for and I've almost solved my problem. Maybe you can help me with the last little bit. Here's my new context:
INSERT INTO target_table(colA, colB, colC, colD, colE)
SELECT seq.NEXTVAL, f.colB, f.colC, f.colD, SYSDATE FROM (SELECT colB, colC, colD FROM source_table); How do I alias the source table as f? Do I just use "FROM source_table f"? What if the from statement involves multiple tables? (sorry, my comments keep submitting even when I haven't finished)
Yes, if you want to alias source_table then use FROM source_table AS f
This worked perfectly in my practical code. Accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.