【问题标题】:ff Function created with compilation errorsff 创建时出现编译错误的函数
【发布时间】:2021-04-24 19:51:39
【问题描述】:
create or replace function cart_jaune(code in equipe.num_eq%type) return integer as    
  nb integer := 0;
begin
  select count(*)
    into nb
    from sanctionner s, joueur j
   where s.code_joueur = j.code_joueur
     and j.num_eq = code
     and s.type_catron like '%jaune%';
  return nb;
end;
/

【问题讨论】:

  • 您收到什么错误消息?顺便说一句,初始化局部变量 nb 是多余的,因为如果没有匹配的记录返回,它将已经产生零。

标签: oracle plsql stored-functions


【解决方案1】:

根据您使用的工具,您应该检查您遇到的哪个错误。因为,有人应该如何提供帮助?无论使用何种工具,都可以查询user_errors。例如:

SQL> create or replace function f_test return number is
  2  begin
  3    retval := 1;         --> variable isn't declared, so - compilation will fail
  4    return retval;
  5  end;
  6  /

Warning: Function created with compilation errors.

SQL> select line, position, text from user_errors where name = 'F_TEST' order by line, position;

 LINE  POSITION TEXT
----- --------- --------------------------------------------------
    3         3 PLS-00201: identifier 'RETVAL' must be declared
    3         3 PL/SQL: Statement ignored
    4         3 PL/SQL: Statement ignored
    4        10 PLS-00201: identifier 'RETVAL' must be declared

或者,在 SQL*Plus 中:

SQL> show err
Errors for FUNCTION F_TEST:

LINE/COL ERROR
-------- -----------------------------------------------------------------
3/3      PL/SQL: Statement ignored
3/3      PLS-00201: identifier 'RETVAL' must be declared
4/3      PL/SQL: Statement ignored
4/10     PLS-00201: identifier 'RETVAL' must be declared
SQL>

所以,您越早告诉我们出了什么问题,我们就能越早提供帮助。


同时,我创建了您在函数中使用的示例表。一切似乎都很好。

SQL> create table equipe(num_eq number);

Table created.

SQL> create table sanctionner (code_joueur number, type_catron varchar2(10));

Table created.

SQL> create table joueur (code_joueur number, num_eq number);

Table created.

功能:我什么都没改,只是稍微格式化了一下:

SQL> create or replace function cart_jaune(code in equipe.num_eq%type)
  2    return integer
  3  as
  4    nb integer := 0;
  5  begin
  6    select count(*)
  7    into nb
  8    from sanctionner  s,
  9         joueur       j
 10    where s.code_joueur = j.code_joueur
 11      and j.num_eq = code
 12      and s.type_catron like '%jaune%';
 13
 14    return nb;
 15  end;
 16  /

Function created.

有效吗?

SQL> select cart_jaune(1) from dual;

CART_JAUNE(1)
-------------
            0

SQL>

是的,它有效。

【讨论】:

    【解决方案2】:

    您传递code 类型为EQUIPE.NUM_EQ%TYPE 的值,但将其与JOUEUR.NUM_EQ 列进行比较。除非您有两个具有相同名称和数据类型的列的表,否则您可能希望将签名中的类型声明为JOUEUR.NUM_EQ%TYPE

    create or replace function cart_jaune(code in joueur.num_eq%type) return integer as    
      nb integer := 0;
    begin
      select count(*)
        into nb
        from sanctionner s, joueur j
       where s.code_joueur = j.code_joueur
         and j.num_eq = code
         and s.type_catron like '%jaune%';
      return nb;
    end;
    /
    

    db小提琴here

    【讨论】:

      【解决方案3】:

      正如其他人所说,您需要显示错误。我们不应该玩“猜猜我口袋里有什么?”的游戏

      也就是说,ansi JOIN 语法已经有 20 年了?你应该使用它:

      select count(*)
        into nb
        from sanctionner  s,
          join joueur j on s.code_joueur = j.code_joueur
        where j.num_eq = code
        and s.type_catron like '%jaune%';
      

      帮助确保您考虑所有的连接。

      【讨论】:

        【解决方案4】:

        既然我们在玩“猜猜我的错误信息”,这里是我的:

        在您的 SQL 查询中,您引用 code:

             and j.num_eq = code
        

        由于您没有为code 提供任何别名,SQL 引擎将首先尝试将其与FROM 子句中列出的任何表中的列相匹配。

        例如,如果sanctionner 有一个列code,那将是SQL 引擎在此处使用的列,即:

        select count(*)
          into nb
          from sanctionner s, joueur j
         where s.code_joueur = j.code_joueur
           and j.num_eq = s.code
           and s.type_catron like '%jaune%';
        

        由于您实际上希望它绑定您的 PL/SQL 函数参数,因此您需要提供别名,即:

        select count(*)
          into nb
          from sanctionner s, joueur j
         where s.code_joueur = j.code_joueur
           and j.num_eq = cart_jaune.code
           and s.type_catron like '%jaune%';
        

        事实上,最好始终为所有列和绑定提供别名,尤其是在将 SQL 嵌入到 PL/SQL 中时。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-10-19
          • 2021-12-07
          • 2017-07-25
          • 2017-01-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多