Ora-00932 - 预计有NUMBER个 -

时间:2017-04-24 13:37:25

标签: oracle plsql oracle11g

我一直在运行以下查询而没有问题:

with Nums (NN) as
(
select 0 as NN
from dual
union all
select NN+1 -- (1)
from Nums
where NN < 30
)

select null as errormsg, trunc(sysdate)-NN as the_date, count(id) as the_count
from Nums
left join
(
    SELECT c1.id,  trunc(c1.c_date)   as c_date
    FROM table1 c1
    where c1.c_date > trunc(sysdate) - 30
    UNION
    SELECT c2.id, trunc(c2.c_date)
    FROM table2 c2
    where c2.c_date > trunc(sysdate) -30
) x1
on x1.c_date =  trunc(sysdate)-Nums.NN
group by trunc(sysdate)-Nums.NN

但是,当我尝试在使用SSRS的proc中弹出它时:

procedure pr_do_the_thing (RefCur out sys_refcursor)

is 
  oops  varchar2(100);
begin

open RefCur for
-- see above query --
;
end pr_do_the_thing;

我得到了

  

错误():PL / SQL:ORA-00932:不一致的数据类型:预期有NUMBER -

有什么想法?就像我上面所说,作为一个查询,没有问题。作为proc,错误出现在注释(1)int eh query。

1 个答案:

答案 0 :(得分:2)

这似乎是错误18139621(参见MOS Doc ID 2003626.1)。有一个补丁可用,但如果这是您遇到此问题的唯一地方,则切换到分层查询可能更简单:

with Nums (NN) as
(
    select level - 1
    from dual
    connect by level <= 31
)
...

您还可以计算CTE内的日期(使用递归CTE也会失败):

with Dates (DD) as
(
    select trunc(sysdate) - level + 1
    from dual
    connect by level <= 31
)
select null as errormsg, DD as the_date, count(id) as the_count
from Dates
left join
(
    SELECT c1.id,  trunc(c1.c_date)   as c_date
    FROM table1 c1
    where c1.c_date > trunc(sysdate) - 30
    UNION
    SELECT c2.id, trunc(c2.c_date)
    FROM table2 c2
    where c2.c_date > trunc(sysdate) -30
) x1
on x1.c_date = DD
group by DD;

我可能会稍微区别一点,因此子查询不会直接限制日期范围:

with dates (dd) as
(
    select trunc(sysdate) - level + 1
    from dual
    connect by level <= 31
) 
select errormsg, the_date, count(id) as the_count
from (
    select null as errormsg, d.dd as the_date, c1.id
    from dates d
    left join table1 c1 on c1.c_date >= d.dd and c1.c_date < d.dd + 1
    union all
    select null as errormsg, d.dd as the_date, c2.id
    from dates d
    left join table2 c2 on c2.c_date >= d.dd and c2.c_date < d.dd + 1
)
group by errormsg, the_date;

但与往常一样,检查每种方法的表现......

另请注意,我已从union切换到union all。如果一个ID可能在同一天,同一个表或两个表中出现多次,那么计数将会有所不同 - 您需要决定是将它们计数一次还是计算它们的次数。这也适用于您的原始查询。