1

我刚开始使用 Oracle BI Publisher,我真的不知道如何处理我的任务。

我的项目是向用户询问一个参数(没关系),如果用户选择 01,我应该只显示“一月”和一些数据。如果用户选择 03,我应该显示 3 列,“一月”“二月”“三月”,每列都有一些数据。

直到 12 月我都有所有的列,我只是不知道如何根据给定的参数选择不同数量的列。

编辑,添加代码!

declare
p SYS_REFCURSOR;

begin

if (:month)=02 then
open p for select 'bazisev' as evtipus, month-1 as month, january, february
    from db.tablename where month=(:month)-1 and sorszam between 4 and 15
  union all
  select 'targyev' as evtipus, month, january, february 
    from db.tablename where month=(:month) and sorszam between 4 and 15
  union all
  select 'targyev/bazisev' as evtipus, month, january, february
    from db.tablename where month=(:month) and sorszam between 32 and 39 
  order by sorszam;

elsif (:month)=01 then

open p for select 'bazisev' as evtipus, month-1 as month, january
    from db.tablename where month=(:month)-1 and sorszam between 4 and 15
  union all
  select 'targyev' as evtipus, month, january
    from db.tablename where month=(:month) and sorszam between 4 and 15
  union all
  select 'targyev/bazisev' as evtipus, month, january
    from db.tablename where month=(:month) and sorszam between 32 and 39 
  order by sorszam;

end if;

end

所以现在我这样做了 2 个月,但它说:ORA-00907:缺少右括号

我很确定我有我需要的所有括号。有什么建议吗?

4

1 回答 1

1

如果用户选择 01,我应该只显示“一月”和一些数据。如果用户选择 03,我应该显示 3 列,“一月”“二月”“三月”,每列都有一些数据。

在纯 SQL 中不能有动态列。您需要使用EXECUTE IMMEDIATE以编程方式(ab)执行此操作。在IF-ELSE构造中,准备动态 SQL,然后根据输入值执行它。

伪代码:您需要注意输出类型。

DECLARE
   v_str VARCHAR2(2000);
BEGIN
   IF :id = 1
      THEN v_str := 'SELECT january FROM table_name';
   ELSIF :id = 2
      THEN v_str := 'SELECT january, february FROM table_name';
   ELSIF...
   .
   .
   .
   ELSIF :id = 12
      THEN v_str := 'SELECT january, february...,december FROM table_name';
   END IF;

   EXECUTE IMMEDIATE v_str...
END;

另一种方法是使用REFCURSOR。例如,

DECLARE

r SYS_REFCURSOR;

BEGIN
   IF :id = 1
      THEN open r for SELECT january FROM table_name;
   ELSIF :id = 2
      THEN open r for SELECT january, february FROM table_name;
   ELSIF...
   .
   .
   .
   ELSIF :id = 12
      THEN open r for SELECT january, february...,december FROM table_name';
   END IF;
END;
于 2015-05-19T09:16:03.357 回答