PL SQL 中的 WHILE 循环有什么问题?

What is wrong with the WHILE Loop on PLSQL?

我正在 PL/SQL 上练习集合,现在我编写了一个简单的 WHILE 循环,用于在屏幕上打印稀疏关联数组的值。我收到以下错误:Oracle 错误 -06502:PL/SQL:数字或值错误:NULL 索引 table 键值。结果打印在屏幕上,但也有 oracle 错误。谢谢

SET SERVEROUTPUT ON

DECLARE

  TYPE type_football_players IS TABLE OF VARCHAR2(45)
       INDEX BY PLS_INTEGER;

  v_costademarfil_2006 type_football_players;

  v_counter PLS_INTEGER;
BEGIN

  v_costademarfil_2006(9)  := 'Kone';
  v_costademarfil_2006(10) := 'Yapi Yapo';
  v_costademarfil_2006(11) := 'Drogba';
  v_costademarfil_2006(14) := 'Kone';
  v_costademarfil_2006(15) := 'Dindane';
  v_costademarfil_2006(19) := 'Toure';
  v_costademarfil_2006(21) := 'Eboue';

  v_counter := v_costademarfil_2006.FIRST;

  WHILE v_costademarfil_2006(v_counter) IS NOT NULL
  LOOP
        DBMS_OUTPUT.PUT_LINE(v_costademarfil_2006(v_counter));

        v_counter := v_costademarfil_2006.NEXT(v_counter);
  END LOOP;
END;

Informe de error - ORA-06502: PL/SQL: numeric or value error: NULL index table key value ORA-06512: at line 48 06502. 00000 - "PL/SQL: numeric or value error%s" *Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2). *Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.

Kone Yapi Yapo Drogba Kone Dindane Toure Eboue

您的 WHILE 循环条件不正确。

您需要检查 v_counter 是否为 NULL,而不是 v_costademarfil_2006(v_counter) 是否为 NULL。

循环的最后一次,v_counter 为 NULL,因此计算 v_costademarfil_2006( NULL )。这导致打印所有名称后出现错误。