【问题标题】:What is the correct way to use nested functions in PLSQL?在 PLSQL 中使用嵌套函数的正确方法是什么?
【发布时间】:2012-08-16 05:49:32
【问题描述】:

我想知道在 PLSQL 中使用嵌套函数的正确方法。在下面的 sn-p 中,函数 'to_number' 工作正常,但我的两个嵌套函数不能以相同的方式使用。

Declare
  Tettt Number;
  Function lolazo(pigt Number, p_Fecha_Fact Date)
  Return Number Is
    resultado number;
  Begin
    Select Vp.Valor
    into resultado
    from valor_precio vp
    Where Vp.Id_Tipo_Cargo = 103
      And Vp.Id_Grupo_Precio = pigt
      and vp.f_inicio_precio =
        (select max(vp2.f_inicio_precio)
         From Valor_Precio Vp2
         Where Vp2.Id_Grupo_Precio = pigt
           And Vp2.F_Inicio_Precio < p_fecha_fact
           And Vp2.Id_Tipo_Cargo = 103 );
    Return Resultado;
  End Lolazo;
  Function Multi(Uno Number, Dos Number) Return Number Is
  Begin
    return uno*dos;
  End Multi;
Begin
  Select to_number('123')
  Into Tettt
  From Dual;

--  Doesn't work!
--  Select lolazo(1544, to_date('01/01/2011','dd/mm/yyyy')) 
--  Into Tettt
--  From Dual;

--  Doesn't work!
--  select Multi(2,3) into Tettt from dual;

Dbms_Output.Put_Line(lolazo(1544, to_date('01/01/2011','dd/mm/yyyy')));
Dbms_Output.Put_Line(Multi(2,3));
dbms_output.put_line(Tettt);
end;

【问题讨论】:

  • 仅供参考:oracle 标签将把它开放给更广泛的群体

标签: oracle function plsql nested-function


【解决方案1】:

你的函数不能在 SQL 中使用,所以不要:select into some_variable from dual

尝试使用作业:

Tettt := Multi(2,3);

另外,从脚本中发布您的错误(而不是说不起作用)。我们更容易看到实际问题。

如果您真的想在 SQL 中使用这些函数,则必须将它们公开给 SQL 引擎(在您的匿名块所在的 PL/SQL 世界之外)。这通常涉及创建单独的存储过程或在某些包规范中定义它们。

希望对您有所帮助。

【讨论】:

  • 谢谢tbone,可惜没有那个可能。我想减少混乱,一些“实用功能”仅在一个特定程序的上下文中有用。我不想为这种情况创建另一个包或创建全局函数,但正如你所说,这是唯一的选择。我正在重构一个古老的代码,其中重复了很多查询,并对限制的值进行了轻微修改,从而自然地将它们传递给函数。
猜你喜欢
  • 2020-06-14
  • 2015-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多