如何解决执行以下Oracle查询时出错
我正在尝试将下面的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','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','5')
AND st.currentClass = sc1.currentClass
AND st.currentCource = sc1.currentCource);
查询返回:ORA-01427单行子查询返回多个行。任何人都可以帮助
解决方法
Oracle在join
语句中不支持update
。您可以改用相关子查询-所有联接所做的都是过滤,因此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
)
,
您可以使用“密钥保留”视图编写此更新,如下所示:
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;
您可以参考此post 我在哪里回答了如何将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','5')
AND st.currentClass = sc1.currentClass
AND st.currentCource = sc1.currentCource);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。