Oracle Reports Builder语言 - 检查查询是否为空



我正在使用Oracle Report Builder 11.1.2.2.0。我的报告中定义的查询很少,我想在其中一个查询没有返回行时执行一些 pl/sql 代码。

所以例如:

if (query returned no rows) then
    do_something();
end if;

如何检查?

您可以尝试使用exception handling将查询转换为function,例如

create of replace function get_color( i_color_id color_palette.id%type ) 
                        return color_palette.fg_color%type is    
  o_color  color_palette.fg_color%type;
begin
  select fg_color
    into o_color
    from color_palette
   where id = i_color_id;
  return o_color;
 exception when no_data_found then return null;
end;

并执行下面的代码

if ( get_color(:id) is null ) then
    paint_it_to_black();
end if;

据我所知,没有办法做到这一点 - 不是以一种简单的方式,也就是说。您必须运行两次相同的查询:一次用于显示结果(如果有(,另一次用于检查该查询是否返回了某些内容。

当然,这意味着它会慢得多(执行相同的查询两次(。

解决方法可能是仅将数据准备到单独的表中(请参阅是否可以使用全局临时表(一次,然后

  • 检查它是否包含任何行
  • 只需select * from that_table(没有任何条件,因为您在将数据插入其中时已经这样做了(

或者,如果您感兴趣的查询简单快捷,只需在PL/SQL过程中使用它即可。您必须在多个位置维护相同的代码。看看你是否可以创建一个返回表的函数 - 这将简化事情(有点(。

最新更新