对于新手问题的道歉,我正在编写一个Oracle存储过程,该存储过程为特定的SQL打开一个游标,为该游标返回的每一行计算一些变量,但该存储过程应作为结果集返回这些已被删除的变量。计算游标返回的每一行。我对如何执行此操作感到困惑-有人可以帮忙吗?!
答案 0 :(得分:0)
到目前为止,我确实读过其中的一些文章(只是经过精简的示例,而不是确切的代码),但是只需要在结果集中返回v_calc和v_calc_res:-
CREATE OR REPLACE procedure sp_test
(
in_input in number,
out_return out sys_refcursor
)
as
v_calc number;
v_calc_res number;
CURSOR C_test IS
select blah from test where blah = in_input;
begin
open c_test
loop
fetch c_test into v_calc;
v_calc_res := v_calc*5;
end loop;
end;
答案 1 :(得分:0)
如果您希望过程返回一个引用游标,以供调用例程使用该过程本身,则不能使用它。光标(包括参考光标)是1路1时间消耗品。至于所需的计算,可以将它们添加以选择为光标定义的内容。所以:
-- setup
create table test (blah integer, blah_stuff varchar2(50) );
-- build sp
create or replace procedure sp_blah_text(
in_input in number
, out_cur out sys_refcursor
)
is
begin
open out_cur for
select blah, blah_stuff, blah*5 as blah_x_5
from test
where blah = in_input;
end sp_blah_text;
-- test data
insert into test(blah, blah_stuff)
select 1,'a' from dual union all
select 2,'b' from dual union all
select 2,'x' from dual union all
select 2,'z' from dual union all
select 3,'c' from dual;
-- test
declare
ref_cur sys_refcursor;
l_blah test.blah%type;
l_stuff test.blah_stuff%type;
l_blah_5 test.blah%type;
begin
dbms_output.enable(null);
sp_blah_text(2,ref_cur);
loop
fetch ref_cur
into l_blah
, l_stuff
, l_blah_5;
exit when ref_cur%notfound;
dbms_output.put_line('blah=' || l_blah || ',stuff=' || l_stuff || ',blah*5=' || l_blah_5);
end loop;
end;
答案 2 :(得分:0)
这很有效,谢谢。我现在遇到一个性能问题,也许您可以帮助解决。当我打开游标时,然后运行其他几个SELECT语句以使用游标中的变量来检索值(请参见下文)。我认为这是因为PL / SQL和SQL引擎之间的切换。使用表集合会有所帮助吗?但是,正如我看到的那样,由于我需要来自不同表的不同列,因此我需要具有多个不同的集合,如何在一条记录中输出所有内容?
CREATE OR REPLACE procedure sp_test
(
in_input in number
)
as
v_calc number;
v_calc_res number;
v_blah_blah number;
v_blah_blah_blah number;
v_blah_blah_blah number;
CURSOR C_test IS
select blah from test where blah = in_input;
begin
open c_test
loop
fetch c_test into v_calc;
select blah_blah into v_blah_blah from t_blah_blah;
select blah_blah_blah into v_blah_blah_blah from t_blah_blah_blah;
select blah_blah_blah_blah into v_blah_blah_blah_blah from t_blah_blah_blah_blah;
v_calc_res := v_calc*5*v_blah_blah*v_blah_blah_blah*v_blah_blah_blah_blah
end loop;
end;