执行以下 Oracle 查询时出错

Error while executing the below Oracle query

我正在尝试将以下 Sybase 查询转换为 Oracle 查询。

update Student set st.age = (case when st.promoted != 'fail' then 1 
else (select sc1.age from school sc1 where st.id = sc1.id ) END)
from Student st ,School sc
where st.id = sc.id
AND st.status in('1','7','2','5')
AND st.currentClass = sc.currentClass 
AND st.currentCource = sc.currentCource ;

但我尝试在转换后执行以下 Oracle 查询,但出现以下错误。

update Student st set st.age = (select (case when st.promoted != 'fail' 
then 1 
else (select sc1.age from school sc1 where st.id = sc1.id ) END) 
from School sc   
where st.id = sc.id
AND st.status in('1','7','2','5')
AND st.currentClass = sc.currentClass 
AND st.currentCource = sc.currentCource )
where exists 
   (select 1 from School sc1
where st.id = sc1.id
AND st.status in('1','7','2','5')
AND st.currentClass = sc1.currentClass 
AND st.currentCource = sc1.currentCource);

查询返回:ORA-01427 单行子查询返回多行。有谁帮忙

Oracle 不支持 update 语句中的 joins。您可以改用相关子查询 - 所有连接所做的都是过滤,所以 exists 应该这样做:

update student st 
set st.age = 20 
where 
    st.status in(1, 7, 2, 5)
    and exists (
        select 1 
        from school sc 
        where 
            st.id = sc.id  
            and st.currentClass = sc.currentClass  
            and st.currentCource = sc.currentCource 
    )

您可以使用Key preserved view来写这个更新如下:

UPDATE (SELECT st.age as age, 
                st.promoted,
                sc.age,
               st.currentClass,
               sc.currentClass,
               st.currentCource,
               sc.currentCource
          FROM Student st INNER JOIN
               School sc
         ON (st.id = sc.id AND st.currentClass = sc.currentClass AND st.currentCource = sc.currentCource)
         WHERE st.status IN ('1','7','2','5'))
   SET age = CASE WHEN st.promoted != 'fail' THEN 1 ELSE sc.age END;

你可以参考这个 我已经回复了如何将 Sybase 更新查询转换为 Oracle。

update Student st set st.age = case when st.promoted != 'fail'
                                    then 1 
                                    else (select sc1.age from school sc1
                                          where st.id = sc1.id )
                                    end
where exists 
   (select 1 from School sc1
where st.id = sc1.id
AND st.status in('1','7','2','5')
AND st.currentClass = sc1.currentClass 
AND st.currentCource = sc1.currentCource);