简体   繁体   中英

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

for the below sql-code i'am getting the following error:

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

update table_result s set (login, position, division, responsibility) = (
select distinct( u.login ), u.position, u.division, u.responsibility from table2 u
where s.user_name=u.login)

I think the error message is clear. You don't want select distinct in the subquery. You want to be sure that it select one row. For that purpose, use rownum :

update table_result s
    set (login, position, division, responsibility) =
             (select u.login, u.position, u.division, u.responsibility
              from table2 u
              where s.user_name = u.login and
                    rownum = 1
             );

Notes:

  • distinct is not a function. What are the parentheses for? They suggest a misunderstanding.
  • Using a subquery like this with rownum = 1 suggests a logical flaw. Which of the matching rows do you really want?

In MySQL: I don't think you can update multiple columns like this. One column to update and the subquery to return one value to update.

like this

update table_result s set login=(select distinct( u.login )from table2  
u where s.user_name=u.login)

I don't know why this is tagged as MySQL when it should be Oracle

That's a wrong syntax. You rather should be using a MERGE statement like below since in Oracle there is no such update-join construct available.

merge into table_result s 
using (
       select distinct  u.login , u.position, u.division, u.responsibility from table2 u
       join table_result s1 on s1.user_name = u.login) xxx
when matched then update
set s.login = xxx.login, 
s.position = xxx.position, 
s.division  = xxx.division, 
s.responsibility = xxx.responsibility;

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