Oracle 如何将ref_游标与用户定义的函数一起使用?
我编写了一个代码来返回一组记录,如下所示Oracle 如何将ref_游标与用户定义的函数一起使用?,oracle,plsql,Oracle,Plsql,我编写了一个代码来返回一组记录,如下所示 CREATE PROCEDURE test (from_dt date, to_dt date, out sys_refcursor) as cursor c is select max(cde) from table1 where to_char(dt, 'yyyymmdd') between from_dt and to_dt group by id; begin for i in c
CREATE PROCEDURE test (from_dt date, to_dt date, out sys_refcursor)
as
cursor c
is
select max(cde)
from table1
where to_char(dt, 'yyyymmdd') between from_dt and to_dt
group by id;
begin
for i in c
loop
select function(i.cde) into v_cde from dual;
open out for select column1, v_cde, column2, column2 from table2
where to_char(dt, 'yyyymmdd') between from_dt and to_dt;
end loop;
end;
执行上述程序后,除v_cde
函数返回值外,所有记录都被正确地重新运行。它为所有记录返回相同的值。
我认为cde
值没有循环。
如何在循环中使用函数和ref_游标
如果有任何人有建议,请告诉我。给定的代码不完整,因为Barbaros正在尝试显示。下面是完成代码的另一种尝试。即使如此,过程测试仍然没有编译,但错误现在减少到ref cursor问题: 行/列错误
16/13 PL/SQL:SQL语句被忽略 16/18 PLS-00361:无法打开输入光标“输出” 对示例的更改:
drop table table1;
drop table table2;
create table table1 (cde number, dt date, id number, from_dt date, to_dt date);
create table table2 (column1 number, v_cde number, column2 number, dt date, id number, from_dt date, to_dt date);
Rem This function returns the same numeric value given
create or replace function func1(arg1 number) return number is
begin
return arg1;
end;
/
show errors;
CREATE or REPLACE PROCEDURE test (from_dt date, to_dt date, rf1 sys_refcursor)
as
v_cde number;
cursor c
is
select max(cde) cde
from table1
where to_char(dt, 'yyyymmdd') between from_dt and to_dt
group by id;
begin
for i in c loop
v_cde := func1(i.cde);
open rf1 for select column1, v_cde, column2, column2 from table2
where to_char(dt, 'yyyymmdd') between from_dt and to_dt;
end loop;
end;
/
show errors;
给定的代码不完整,因为Barbaros正在尝试显示。下面是完成代码的另一种尝试。即使如此,过程测试仍然没有编译,但错误现在减少到ref cursor问题: 行/列错误
16/13 PL/SQL:SQL语句被忽略 16/18 PLS-00361:无法打开输入光标“输出” 对示例的更改:
drop table table1;
drop table table2;
create table table1 (cde number, dt date, id number, from_dt date, to_dt date);
create table table2 (column1 number, v_cde number, column2 number, dt date, id number, from_dt date, to_dt date);
Rem This function returns the same numeric value given
create or replace function func1(arg1 number) return number is
begin
return arg1;
end;
/
show errors;
CREATE or REPLACE PROCEDURE test (from_dt date, to_dt date, rf1 sys_refcursor)
as
v_cde number;
cursor c
is
select max(cde) cde
from table1
where to_char(dt, 'yyyymmdd') between from_dt and to_dt
group by id;
begin
for i in c loop
v_cde := func1(i.cde);
open rf1 for select column1, v_cde, column2, column2 from table2
where to_char(dt, 'yyyymmdd') between from_dt and to_dt;
end loop;
end;
/
show errors;
你确定这个过程是编译的吗?是的…过程是编译的。请看一下。这个存储过程有很多错误。首先,为什么要在循环中多次打开ref游标?假设存在最后一次迭代,您将得到循环最后一次迭代的结果。表
table1
和table2
的关系如何?为什么光标c
没有从表1
中选择id
列?它提供了一组cde
的最大值,按id
分组,而不告诉您每个最大值对应的id
。此外,名为out
的参数是IN
参数:如果要从进程返回它,必须声明它out sys\u refcursor
。但最大的问题是,为什么要在光标上循环?我强烈怀疑这个存储过程的整个主体可能会被一个查询所取代,您可以打开输出引用游标。您确定这个过程会编译吗?是的…过程会编译。请看一下。这个存储过程有很多错误。首先,为什么要在循环中多次打开ref游标?假设存在最后一次迭代,您将得到循环最后一次迭代的结果。表table1
和table2
的关系如何?为什么光标c
没有从表1
中选择id
列?它提供了一组cde
的最大值,按id
分组,而不告诉您每个最大值对应的id
。此外,名为out
的参数是IN
参数:如果要从进程返回它,必须声明它out sys\u refcursor
。但最大的问题是,为什么要在光标上循环?我强烈怀疑这个存储过程的整个主体可能会被一个查询所取代,您可以为该查询打开output ref游标。