简体   繁体   中英

Oracle SQL error - Single-row subquery returns more than one row

    UPDATE Table_A
SET Column_A = 
(SELECT Table_C.Column_C
FROM
    Table_A
INNER JOIN  
    Table_B
ON
    Table_A.Column_A1 = Table_B.Column_B
INNER JOIN
    Table_C
ON
    Table_B.Column_B1 = Table_C.Column_C1
WHERE
   Table_C.Column_C2 <> 'T' and 
   Table_C.Column_C3 = 'T' and 
   Table_B.Column_B2 = 'T' and Table_B.Column_B3 = 'xyz');

I've a subquery in the brackets returning 10 values which I want to update Table_A with. But the update statement only wants one value being returned from the sub-query:

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

How could I update several rows instead of just one?

Any threads I've seen on this error do not help resolve the issue.

EDITED - Would there be a way to do this using a loop?

You need to correlate the subquery to the row in the main query:

UPDATE Table_A a
SET Column_A = 
(SELECT Table_C.Column_C
FROM
    Table_A
INNER JOIN  
    Table_B
ON
    Table_A.Column_A1 = Table_B.Column_B
INNER JOIN
    Table_C
ON
    Table_B.Column_B1 = Table_C.Column_C1
WHERE
   Table_C.Column_C2 <> 'T' and 
   Table_C.Column_C3 = 'T' and 
   Table_B.Column_B2 = 'T' and Table_B.Column_B3 = 'xyz'
   AND a.PKCOLS=Table_A.PKCOLS);

("PKCOLS" being whatever column(s) are the primary key of Table_A.)

If the correlated subquery still returns more than one row then you will need to decide which of the multiple returned rows you want and add something so that only that one is returned - eg the one with the highest creation date or whatever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM