 |
|
ORA-01427: single-row subquery returns more than one row tips
Oracle Error Tips by Donald Burleson(S. Karam)
|
The Oracle docs note this on the
ORA-01427 error*:
- ORA-01427
single-row subquery returns more than one row
- Cause:
The outer query must use one of the keywords ANY, ALL, IN, or NOT IN to
specify values to compare because the subquery returned more than one
row.
-
- Action:
Use ANY, ALL, IN, or NOT IN to specify which values to compare or reword
the query so only one row is retrieved.
On
Oracle Server and PL/SQL User Forum, Adam Wilbur received an ORA-01427 error
code and received this information:
Question
Posted by 'Adam Wilbur' on April 21, 2001 at 20:37:00 EST:
Here's what I am trying to do...
UPDATE TABLEA SET TABLEA.COLUMN1 =
(select TABLEB.COLUMN1 FROM TABLEA, TABLEB
WHERE TABLEB.COLUMN2 = 'String Value'
AND TABLEA.COLUMN3 = TABLEB.COLUMN3
AND TABLEA.COLUIMN4 = TABLEB.COLUMN4
AND TABLEB.VALUENAME = 'String Value');
This query returns multiple rows, all of which I need to update in TABLEA.
How do I do this without getting ORA-01427 error?
Answer
Dmytro offered this advice to resolve ORA-01427:
Try to add and rownum=1 to your subquery conditions if you DO NOT care
about the value from the list or DO sure that they are the same.
Single Row Subquery
A single row subquery returns only one row. It can be
used with the equal comparison operators (=,<,>,<>, etc).
SQL> select
2
order_number
3 from
4 sales
5 where quantity = (select
6
max(quantity)
7 from
8 sales);
ORDER_NUMBER
--------------------
O161
Here, we get the order number for the order that
contains the max quantity value. The outer query can return multiply rows,
but the subquery can only return one row.
SQL> select
2
order_number
3 from
4 sales
5 where quantity > (select
6
avg(quantity)
7 from
8 sales);
ORDER_NUMBER
--------------------
O118
O149
O157
O161
O167
O179
O183
O186
O189
O193
O196
O197
O198
O199
14 rows selected.
Here, we want the order numbers where the order quantity
is above the average quantity in the sales table. Notice that the subquery
returns one and only one row that used in the filter.
Helpful Links:
You may want to refer to these helpful links concerning
single and multi row subqueries, and tips on
deleting
rows while avoiding ORA-01427 by Don Burleson.