我们有一个select
语句,根据开关的不同而不同。然后我们循环结果。
目前代码如下:
if switch_a then
for rec in
(
select *
order by decode(x, y,1,0),
decode(x,a,2,0),
decode(x,z,3,0), pos
)
loop ...
end loop;
else
for rec in
(
select *
order by decode(x, y,1,0),
decode(x,z,3,0), pos
)
loop ...
end loop;
end if;
现在我想将该代码转换为仅一个循环。所以我选择:
if (switch_a) then
Statement call := select*...
else
Statement call := select*...
end if;
for rec in call
loop
end loop;
问题是,我不知道这是否适用于录制,我不知道如何定义可执行的SQL语句。这是可能的吗?如果可以的话,你能举个例子吗?
答案 0 :(得分:0)
可能是这样的:
declare
Statement_call varchar2(4000);
rec sys_refcursor;
l_row number;
begin
if (switch_a) then
Statement_call := 'select 1 as q from dual';
else
Statement_call := 'select 2 as q from dual';
end if;
OPEN rec FOR Statement_call;
loop
FETCH rec INTO l_row;
EXIT WHEN rec%NOTFOUND;
dbms_output.put_line(l_row);
end loop;
CLOSE rec;
end;