如何在游标中调用函数,游标是 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;

我希望光标 c1 使用 f1 函数返回数据。但它 说

错误:PLS-00231:函数"f1"可能未在 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;

最新更新