代码之家  ›  专栏  ›  技术社区  ›  Thomas Leplus

PLS-00306:过程调用中的参数数量或类型错误

  •  0
  • Thomas Leplus  · 技术社区  · 1 年前

    以下SQL导致Oracle 11g错误: PLS-00306: wrong number or types of arguments in call to 'PRINT' 。但在我看来,它就像我的 print 过程需要1个varchar2参数,而这正是我作为参数传递给它的内容。

    CREATE TABLE employees (full_name VARCHAR2(64))
    //
    
    INSERT INTO employees (full_name) VALUES ('John')
    //
    
    INSERT INTO employees (full_name) VALUES ('Paul')
    //
    
    INSERT INTO employees (full_name) VALUES ('George')
    //
    
    INSERT INTO employees (full_name) VALUES ('Ringo')
    //
    
    CREATE OR REPLACE PROCEDURE print (
      v_string IN VARCHAR2
    ) IS
    BEGIN
      dbms_output.put_line(v_string);
    END print;
    //
    
    DECLARE
      v_string VARCHAR2(64);
    BEGIN
        FOR v_string IN (SELECT DISTINCT full_name
                                FROM  employees) LOOP
            print (v_string);
        END LOOP;
    END;
    //
    

    我可以在SQLFiddle中重现这个问题: http://sqlfiddle.com/#!4/c0e80e/6

    我错过了什么?

    谢谢

    1 回复  |  直到 1 年前
        1
  •  1
  •   Littlefoot    1 年前

    没错,你做错了。应该是

    SQL> BEGIN
      2      FOR v_string IN (SELECT DISTINCT full_name
      3                              FROM  employees) LOOP
      4          p_print (v_string.full_name);
      5      END LOOP;
      6  END;
      7  /
    John
    Paul
    George
    Ringo
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    为什么?因为您声明了局部变量并使用了游标 FOR 循环(其记录与本地变量同名)。你传给了什么 print 过程是一个游标变量,而不是局部变量。

    documentation 说:

    游标FOR LOOP语句隐式声明其循环索引为指定游标返回的行类型的记录变量,然后打开游标。

    语法为: for record in ... 哪里 record 代表

    游标for loop语句隐式声明为游标或select_statement返回类型的%ROWTYPE记录变量的循环索引的名称。