将varray的大小设置为count SQL语句的结果

时间:2017-05-11 14:06:34

标签: oracle plsql

我试图在select

中存储一个相当简单的varray语句的结果
select id from my_table where column1 is not null;

理想情况下,我要声明我的varray并将其大小设置为

select count(id) from my_table where column1 is not null;
像这样的伪代码:

type my_array_type is varray(select count(id) from my_table where column1 is not null) of int;

my_array my_array_type := my_array_type();

my_array.extend(select count(id) from my_table where column is not null);

FOR i IN 1..my_array.count LOOP    
  my_array(i) := <get id from select statement using cursor>;
END LOOP;

对于我想要达到的目标,是否有更好的方法?

1 个答案:

答案 0 :(得分:1)

无法使用varray进行动态大小初始化,您可以尝试使用嵌套表而不是它。

(参考Declare dynamic array in Oracle PL/SQL

请检查下面提到的示例代码。

DECLARE
    type my_array_type is TABLE  of MY_TABLE.COLUMN1%TYPE;

    l_employees my_array_type;
BEGIN
-- All rows at once...
  SELECT COLUMN1      
    BULK COLLECT INTO l_employees
    FROM MY_TABLE
   ORDER BY COLUMN1;

  DBMS_OUTPUT.put_line (l_employees.COUNT);

  FOR indx IN 1 .. l_employees.COUNT
  LOOP
     DBMS_OUTPUT.put_line (l_employees(indx));
  END LOOP;
END;

希望这会对你有帮助!