0

I want to ad column by using subquery.

Insert into Table_name
values (1, 'a', 'b', sysdate, sysdate + 120, 'c',
        (Select number from other_table where column_name = 'x'), 2);

Error:

ORA-01427: single-row subquery returns more than one row.

How can I solve it?

1
  • Depends on the number column. Please share some sample data. Commented Jul 11, 2022 at 19:38

2 Answers 2

2

I think you want to use an INSERT INTO ... SELECT here. Note that generally you should always specify the target columns.

INSERT INTO Table_name (c1, c2, c3, c4, c5, c6, c7, c8)
SELECT 1, 'a', 'b', sysdate, sysdate + 120, 'c', number, 2
FROM other_table
WHERE column_name = 'x';
Sign up to request clarification or add additional context in comments.

Comments

0

you need to force a single row

note the extra "ROWNUM" below

something like:

Insert into Table_name values(1,'a','b',sysdate,sysdate + 120,'c',(Select number from other_table where column_name = 'x' and ROWNUM = 1),2);

see:https://blogs.oracle.com/connect/post/on-rownum-and-limiting-results

4 Comments

Just getting the top one would not be the best idea, maybe the answer is to limit it on another where condition.
sorry. it should be where/and, instead of 2 wheres.
This query will not resolve the issue, but just remove an error. Because it will return an arbitrary row and will never fail even in case of change in the data model, making it hard to locate the problematic code.
Then your where clause on your "inner-select" needs to be more specific. If you are trying to find a specific ROW with your "inner-select" you need to add more information in your question.

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.