Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 如何将ref_游标与用户定义的函数一起使用?_Oracle_Plsql - Fatal编程技术网

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:无法打开输入光标“输出”

对示例的更改:

  • 表1和表2都是用我认为是列来定义的
  • 函数是在模式级别定义的
  • 现在定义了局部变量v_cde
  • 函数现在是在模式级别定义的
  • 下面是一个新的例子:

    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:无法打开输入光标“输出”

    对示例的更改:

  • 表1和表2都是用我认为是列来定义的
  • 函数是在模式级别定义的
  • 现在定义了局部变量v_cde
  • 函数现在是在模式级别定义的
  • 下面是一个新的例子:

    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游标。