如何调用游标中的函数,游标是 oracle 中另一个过程的一部分

How to call a function in a cursor, which is a part of another procedure in oracle

我有一个这样的存储过程

create or replace procedure A is
  procedure a1 is
  ......
  end;

  procedure a2 is
   cursor c1 as
    select a,b,(select f1(x,y) var_temp from dual)data from table_a; -- error here says
                       --Error: PLS-00231: function 'f1' may not be used in SQL
   begin 
      ......
   end;

  function f1(x varchar2,y varchar2) return varchar2 is
  .....
  end;
begin
....................
end;

我希望使用 f1 函数将游标 c1 return 数据..但是它 说

Error: PLS-00231: function 'f1' may not be used in SQL..

创建一个包可以解决这个问题,但我必须只在一个过程中做...

问题是,如错误所述,您不能在 SQL 语句中使用匿名块中定义的函数,并且该函数在定义之前就已被使用。

您可以做的是在使用前移动定义并按原样从游标中获取数据并在循环时对值应用函数:

create or replace procedure A is
  procedure a1 is
  ......
  end;

  function f1(x varchar2,y varchar2) return varchar2 is
  .....
  end;

  procedure a2 is
   cursor c1 as
    select a,b from table_a;
   begin
       for i in c1 loop
           -- use f1(i.a, i.b) here
           dbms_output.put_line(f1(i.a, i.b));
       end loop;
   end;
begin
....................
end;