我刚从Oracle BI Publisher开始,我真的不知道如何与我的任务相处。
我的项目是询问用户参数(确定),如果用户选择01,我应该只显示' 1月'有一些数据。如果用户选择03,我应该显示3列,' 1月' '二月' '行军'每个都有一些数据。
我的所有专栏都在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:错过右括号
我很确定我拥有所需的所有括号。有什么建议吗?
答案 0 :(得分:1)
如果用户选择01,我应该只显示'january'和一些数据。如果用户选择03,我应该显示3列,“1月”,“2月”,“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;