【问题标题】:Error while executing the below Oracle query执行以下 Oracle 查询时出错
【发布时间】:2020-12-25 15:00:27
【问题描述】:

我正在尝试将下面的 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 单行子查询返回多于一行。有谁请帮忙

【问题讨论】:

    标签: sql oracle join sql-update subquery


    【解决方案1】:

    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 
        )
    

    【讨论】:

    • 使用 case 语句更新了值,现在您的查询需要进行任何更改
    【解决方案2】:

    您可以使用密钥保留视图来编写此更新,如下所示:

    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。

    【讨论】:

      【解决方案3】:
      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);
      

      【讨论】:

      • 我已经更新了20个case语句,现在如何转换
      • 我已经尝试过同样的方法来转换这个 sybase 查询,但没有工作:你能帮我update Product set pd.age = (case when pd.exittime!= null then 1 else ( case when pd.queue = dp.queue then (select (sysdate - pd.exittime) from department dp1 where pd.id = dp1.id ) else 2 END) END) from Product pd,department dp where pd.id > 1 AND pd.id = dp.id AND pd.status in('1','7','2','5') AND pd.currentstatus = dp.currentstatus AND pd.activity= dp.activity;
      • 您需要指定别名:update Product pd set...
      • 您仍在尝试使用新查询中的联接进行更新 - 您尚未转换它。
      猜你喜欢
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-03
      • 2018-12-14
      相关资源
      最近更新 更多