ORA-01427:单行子查询在CASE语句中返回多行

时间:2011-08-11 15:07:28

标签: sql oracle

我使用以下代码收到ORA-01427错误:

update rpt_group a
set a.rpt_category_id = 
case
  when ((select c.control from grpmisc c
 where (c.grp = a.grp)) = '01') then '201'
  when ((select c.control from grpmisc c
 where (c.grp = a.grp)) = '02') then '202'
  when ((select c.control from grpmisc c
 where (c.grp = a.grp)) = '03') then '203'
 else '93'
   end
where rpt_category_id = '93';

但是当我这么做时,请说

select c.control from grpmisc c, rpt_group a
 where c.grp = a.grp and a.grp = '01'

它不返回任何行。对于'02'和'03',这是相同的。那么为什么我会得到“子查询返回多行”错误?

谢谢..

2 个答案:

答案 0 :(得分:1)

你正在进行这项测试......
select c.control from grpmisc c, rpt_group a where c.grp = a.grp and a.grp = '01'

但你的子查询是在这个...形成的 select c.control from grpmisc c, rpt_group a where c.grp = a.grp

如果你运行两个查询中的后一个,我打赌你会得到很多记录?这意味着您要么丢失子查询中的and a.grp = '01',要么将结果限制为仅一条记录...


有两种选择可能......

(select c.control from grpmisc c where c.grp = a.grp group by c.control)

(select c.control from grpmisc c where c.grp = a.grp and rownum < 2 order by <blah>)

答案 1 :(得分:0)

代码中至少有三个位置可能会出现此类错误。 你应该限制子查询,这样他们只能返回一行。例如

...
case
  when ((select c.control from grpmisc c
 where (c.grp = a.grp) and rownum < 2) = '01') then '201'
...