我在游标中有一个简单的查询
Cursor some_cursor IS
select
sum(some_field)
from some_table table_1
where
table_1.TYPE =1
AND TO_CHAR(table_1.date,'YYYYMMDD') = '20090905'
AND table_1.f2 = 962
AND table_1.f3 = 41813;
然后我做
fetch some_cursor into some_var;--some_var is of type Number, cursor is open
当我运行此查询时,some_var有可能为NULL。在这种情况下,id喜欢它取0值;
像
这样的东西 --C like pseudocode of what I want
(some_cursor!=Null?(fetch some_cursor into some_var):(some_var:=0))
有办法做到这一点吗?我正在考虑将上述查询重写为
Cursor some_cursor IS
select
sum(some_field),count(*)
from some_table table_1
where
table_1.TYPE =1
AND TO_CHAR(table_1.date,'YYYYMMDD') = '20090905'
AND table_1.f2 = 962
AND table_1.f3 = 41813;
然后写
fetch some_cursor into some_var,some_counter;
if (some_counter = 0) then
begin
some_var :=0;
end
但这意味着重写10个游标(是的,没有那么多)。也许plsql有一个更清洁的方式。
提前致谢
答案 0 :(得分:4)
尝试:
SELECT NVL(SUM(some_field),0)
from some_table table_1
where table_1.TYPE =1
AND TO_CHAR(table_1.date,'YYYYMMDD') = '20090905'
AND table_1.f2 = 962
AND table_1.f3 = 41813;
答案 1 :(得分:1)
当你在那里时,你可能会纠正其他几个问题:
因此:
Declare
some_var Number;
Begin
select Coalesce(sum(some_field),0)
into some_var
from some_table table_1
where table_1.TYPE = 1
AND table_1.date >= date '2009-09-05'
AND table_1.date < date '2009-09-06'
AND table_1.f2 = 962
AND table_1.f3 = 41813;
End;