【问题标题】:How to call a function in a cursor, which is a part of another procedure in oracle如何在游标中调用函数,游标是oracle中另一个过程的一部分
【发布时间】:2017-03-20 19:46:00
【问题描述】:

我有一个这样的存储过程

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 中使用..

创建一个包可以解决这个问题,但我只能在一个过程中完成......

【问题讨论】:

  • 嗨,GurV,我在程序之前定义了函数,即在我的情况下是在 p3 之前。我还尝试在 Cursor 之后在 P3 的声明部分中声明该函数。但同样的问题..

标签: sql oracle stored-procedures plsql stored-functions


【解决方案1】:

问题是,正如错误所说,您不能在 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;

【讨论】:

  • 非常感谢Gurv,是的,这有效。当我试图调用一个匿名函数时,它总是在 sql 语句中调用时抛出 sql 错误,否则它很好。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-16
  • 2010-10-26
相关资源
最近更新 更多