简体   繁体   中英

Oracle - single-row subquery returns more than one row

I have a query which I'm trying to get it working on oracle but get the following error

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

The query is:

UPDATE TARGET A
SET A.COL1=(SELECT B.COL1 FROM SOURCE B) 
WHERE A.COL2=(SELECT B.COL2 FROM SOURCE B)

Basically, I am trying to update only those records which match in the join.

I'd be extremely grateful for any help.

Your UPDATE statement needs to be a correlated update-- you need some key in A that tells you which row in B to go to in order to retrieve the new COL1 value. Your WHERE clause should also be an IN or an EXISTS since it needs to return multiple rows. Assuming COL2 is the key (based on your WHERE clause), I'm guessing that you want

UPDATE target a
   SET a.col1 = (SELECT b.col1
                   FROM source b
                  WHERE a.col2 = b.col2)
 WHERE EXISTS( SELECT 1
                 FROM source b
                WHERE a.col2 = b.col2 )

If that is not what you're looking for, posting some table definitions, sample data, and the expected output would be quite helpful.

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