ORA-01427
There're two error patterns related to ORA-01427 described in this post:
I will talk about them respectively in the following sections. In which, the first error pattern is very common and easy to solve. But the second one is not so obvious, you need more patience to solve it.
ORA-01427 in SELECT with Equal Operator
If SQL engine expects your subquery to return a single row, it may throw ORA-01427 when the subquery returns more than one row unexpectedly. For example:
SQL> select * from employees where department_id = (select department_id from departments where location_id = 1700);
select * from employees where department_id = (select department_id from departments where location_id = 1700)
*
ERROR at line 1:
ORA-01427: single-row subquery returns more than one row
This is because the subquery in the SELECT statement returned more than one row for the predicate department_id, which does not comply with a singular value limited operator, the equal =. Consequently, the statement fails to continue and then throw ORA-01427 to notify developers.
Solutions
It's just like that we wanted strictly only one item, but eventually it returned more than we expected.
1. Using IN Operator
Then what operator is used to prevent ORA-01427 in SELECT statement? In practice, SELECT should use IN operator instead of = (equal operator) in order to accommodate more than one row returned by the subquery.
SQL> select * from employees where department_id in (select department_id from departments where location_id = 1700);
A similar exception that relates to returned number mismatch is ORA-00913: too many values. But they have different error patterns.
More comparison conditions like ANY, SOME or ALL should also be helpful to accept more than one row in your statements so as to avoid ORA-01427 in subquery statement.
2. Using = (Equal) Operator
If you really want to use an equal operator to confine the scope of returned rows as one, you have to limit the number of rows returned of queries to only one row. That's how we workaround it.
SQL> select * from employees where department_id = (select * from (select department_id from departments where location_id = 1700) where rownum = 1);
An order by clause is more appropriate in the above subquery so as to be close to your expectation.
select * from employees where department_id = (select * from (select department_id from departments where location_id = 1700 order by manager_id) where rownum = 1);
3. Do Not Use DISTINCT with Equal Operator
Adding one distinct keyword before column list cannot prevent ORA-01427, because the number of rows returned by the subquery is still unpredictable.
SQL> select * from employees where department_id = (select distinct department_id from departments where location_id = 1700);
select * from employees where department_id = (select distinct department_id from departments where location_id = 1700)
*
ERROR at line 1:
ORA-01427: single-row subquery returns more than one row
No surprises, we saw ORA-01427 in SELECT statement once again.
ORA-01427 in Job Failed by Trigger
Same error ORA-01427 could accompany ORA-12012 in the alert log when one scheduled job failed to complete successfully.
ORA-12012: error on auto execute of job 10
ORA-01427: single-row subquery returns more than one row
ORA-06512: at line 21
Trigger Caused ORA-01427
According to this error pattern in the above, we saw ORA-01427 in a failed job with ORA-12012. We'd better check some triggers, especially logon and logoff ones to see if there's any chances to block the job process accidentally. Noticeably, which is Job No. 10 in this case.
You should check the content of the job in the first place to see if there're any chances to throw ORA-01427. Perhaps you should disable each trigger at a time in order to isolate and identify the cause. Or just turn them off temporarily.
Further reading: How to Kill Session in Logon Trigger
MySQL Subquery Returns More Than 1 Row
The same error pattern occurs in MySQL, incorrect number of rows from subquery will result ERROR 1241, let's see its content:
ERROR 1242 (ER_SUBSELECT_NO_1_ROW)
SQLSTATE = 21000
Message = "Subquery returns more than 1 row"
This error is caused by the subquery that must return at most one row but returns multiple rows eventually.
mysql> select * from items where id = (select item_id from orders);
If the subquery returns just one row, the above query will work without errors. If it returns more than one row, we will see error 1242.
The solution is the same as we mentioned in the above sections. Beside IN operator, we can also use ANY, one of comparison conditions to fix the query, for example:
mysql> select * from items where id = any (select item_id from orders);
This is how comparison conditions work for MySQL.
Hello I have ora-1427 error only when I’m trying to export the results from sqldevelopper result-grid.
But it displays every rows in the grid.
What can be the problem?
HI
I am using oracle sql , trying to return results from database using below query
select ORG_REF ,
(Select COUNT (POSITION_TYPE) from Table name where POSITION_TYPE LIKE’BASELOAD’ group by ORG_REF)
from table name ORDER BY ORG_REF ;
and getting below error – please advise to resolve it
ORA-01427: single-row subquery returns more than one row
The error means that your subquery returns multiple rows, where only one row is expected. I think you should rewrite your subquery.
update pay_details set bookrecord=(select reverse(bookrecord) from pay_details);
ERROR at line 1:
ORA-01427: single-row subquery returns more than one row
please someone help me
Which means that your inner query select reverse(bookrecord) from pay_details returns more than one row.
sure
when I execute this querey : select reverse(bookrecord) from pay_details
it works
Try to use DISTINCT, it may have chances to condense the result into only 1 row.
update pay_details set bookrecord=(select * from (select distinct reverse(bookrecord)) from pay_details);
If it doesn’t work, the ultimate solution is to make it return only 1 row. Just like we’ve talked in the post.
update pay_details set bookrecord=(select * from (select reverse(bookrecord) from pay_details) where rownum = 1);