识别单行子查询错误

时间:2014-01-13 18:41:57

标签: sql oracle subquery

我有一个Oracle SQL查询抛出错误:

ORA-01427: single-row subquery returns more than one row
01427. 00000 -  "single-row subquery returns more than one row"
*Cause:    
*Action:

我该怎么做才能避免此错误?如何确定此错误的原因/来源?

这是SQL查询:

  select (select CID 
          from SPL A
          where A.prodid = appl.prod_id
                    and A.STATUS = 'SET'
                    and A.DT = (select min(DT) from SPL B where A.prodid = B.prodid))
            as CIDORIG
  from prod_master appl
  where prod_status = 'OFF';

但是,我无法在我的SQL语句中找到这样的子查询。 MIN()只返回一个结果。我也尝试用关键字IN替换'='符号,但没有任何运气。此查询适用于其他prod_status值。

在抛出错误之前,它从我们期望的15,648行中获取了13,700行。我期待15,648行,因为SQL Developer配置为一次只返回50行。当我选择“Count Rows”选项时,它会给出数字15,648。

SPL和prod_master都是观点。

2 个答案:

答案 0 :(得分:3)

据推测,返回多行的子查询是:

     (select CID 
      from SPL A
      where A.prodid = appl.prod_id
                and A.STATUS = 'SET'
                and A.DT = (select min(DT) from SPL B where A.prodid = B.prodid)
     ) as CIDORIG

要解决此问题,请尝试选择min(CID)max(CID)

答案 1 :(得分:0)

仅当您确定子查询将返回1行或0行时才使用标量子查询,否则您将最终出现此错误。

您可以在不使用标量子查询的情况下为您编写查询,如下所示

select a.CID 
  from prod_master appl
       join SPL A
         on ( A.prodid = appl.prod_id)
  where appl.prod_status = 'OFF'
    and A.STATUS = 'SET'
    and A.DT = (select min(b.DT) 
                  from SPL B 
                 where A.prodid = B.prodid);
相关问题