user3224907
user3224907

Reputation: 768

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.

Upvotes: 1

Views: 6784

Answers (1)

Justin Cave
Justin Cave

Reputation: 231881

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.

Upvotes: 3

Related Questions