PLSQL Block,Ifnull这句话是否可能?

时间:2016-03-09 18:12:11

标签: plsql

我有一个从空表查询的PL / SQL块。我想知道有没有办法将空白结果转换为0?或至少我们这个查询的ifnull函数?

这是代码

declare 
p_entity_id varchar2(14) := 10100033904;
date_format varchar2(10) := 'MM/DD/YYYY';
v_mapcount integer;
v_compcount integer;
v_qa_comp_date varchar2(12) := 'MM/DD/YYYY';


begin 

SELECT SUM(total),SUM(complete),TO_CHAR(MAX(compDate),date_format) INTO v_mapCount,v_CompCount,v_qa_comp_date  FROM
(
SELECT COUNT(barcode) as total, SUM(DECODE(qa_comp,NULL,0,1)) complete ,MAX(qa_comp) compDate  from bbsp_boundary  WHERE upper(cycle1) = 'Y' AND  entity_id = p_entity_id AND DELETED_DATE IS NULL
    UNION ALL
SELECT COUNT(barcode) as total, SUM(DECODE(qa_comp,NULL,0,1)) complete ,MAX(qa_comp) compDate  from bbsp_feature WHERE upper(cycle1) = 'Y' AND  entity_id = p_entity_id AND DELETED_DATE IS NULL
    UNION ALL
SELECT COUNT(barcode) as total, SUM(DECODE(qa_comp,NULL,0,1)) complete ,MAX(qa_comp) compDate  from bbsp_ADDRESS WHERE upper(cycle1) = 'Y' AND  entity_id = p_entity_id AND DELETED_DATE IS NULL
    UNION ALL
SELECT COUNT(barcode) as total, SUM(DECODE(qa_comp,NULL,0,1)) complete ,MAX(qa_comp) compDate from bbsp_CORRECTION WHERE upper(cycle1) = 'Y' AND  entity_id = p_entity_id AND DELETED_DATE IS NULL
    UNION ALL
SELECT COUNT(barcode) as total, SUM(DECODE(qa_comp,NULL,0,1)) complete ,MAX(qa_comp) compDate  from bbsp_point_landmark WHERE upper(cycle1) = 'Y' AND  entity_id = p_entity_id AND DELETED_DATE IS NULL
    UNION ALL
SELECT COUNT(barcode) as total, SUM(DECODE(qa_comp,NULL,0,1)) complete ,MAX(qa_comp) compDate  from bbsp_area_landmark WHERE upper(cycle1) = 'Y' AND  entity_id = p_entity_id AND DELETED_DATE IS NULL
) ;

end;

2 个答案:

答案 0 :(得分:1)

如果所有表都为空,则不返回任何行,因此没有计数返回。您可以使用if .. then ... end将这些变量重置为零,或者在select语句后添加以下内容:

   v_mapcount       := COALESCE (v_mapcount, 0);
   v_compcount      := COALESCE (v_compcount, 0);
   v_qa_comp_date   := COALESCE (v_qa_comp_date, 0);

如果Coalesce不为null,则返回第一个值,否则返回第二个值。

答案 1 :(得分:0)

尝试nvl()功能

例如:nvl(sum(total),0)