表类型变量在oracle中用作循环索引

时间:2016-12-05 13:32:40

标签: oracle loops types

我创建了表格类型

 create or replace TYPE NUMBER_ARRAY 
    AS TABLE OF NUMBER;

我想在循环中逐个选择PERIODS

 DECLARE PERIODS NUMBER_ARRAY;
 BEGIN
   FOR P IN(SELECT * FROM TABLE (PERIODS))
   LOOP
     SYS.DBMS_OUTPUT.PUT_LINE(p);
   END LOOP;
 END;

我收到此错误

  

调用'PUT_LINE'时参数的数目或类型错误

2 个答案:

答案 0 :(得分:1)

P属于record数据类型。 put_line()过程需要字符串或可以隐式或显式转换为一个的任何其他值。查询简单嵌套表集合时,只返回一个名为COLUMN_VALUE的列。因此,在将所有内容放在一起后,您的代码应如下所示:

 DECLARE PERIODS NUMBER_ARRAY;
 BEGIN
   FOR P IN (SELECT * FROM TABLE (PERIODS))
   LOOP
     /*
      Since elements of the collection are of NUMBER data type
      it's better to convert them to a character explicitly
      rather than allow Oracle to do it implicitly, thus the use
      of TO_CHAR() function.
     */
     SYS.DBMS_OUTPUT.PUT_LINE(to_char(p.column_value));
   END LOOP;
 END;

答案 1 :(得分:1)

试试这个:

 DECLARE PERIODS NUMBER_ARRAY;
 BEGIN
 --PERIODS := NUMBER_ARRAY (1, 2, 3); example for you
 FOR P IN 
 (
 SELECT * FROM TABLE (PERIODS)
 )
 LOOP
 DBMS_OUTPUT.PUT_LINE(P.column_value );
 END LOOP;
 END;