如何在数组中存储select查询结果列?

时间:2016-06-16 11:47:18

标签: oracle plsql

如果我们在类型编号的表中有一个列,我们如何在数组中的该列上存储select查询的结果?

4 个答案:

答案 0 :(得分:3)

创建一个存储编号的类型: -

CREATE OR REPLACE TYPE varray is table of number;

- 将你的选择查询写入loop(),我将通过级别

进行提取
 declare
      p  varray := varray();
    BEGIN
      for i in (select level from dual connect by level <= 10) loop
        p.extend;
        p(p.count) := i.level;
      end loop;
      for xarr in (select column_value from table(cast(p as varray))) loop
         dbms_output.put_line(xarr.column_value);
     end loop;
  END;

输出: -

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

答案 1 :(得分:3)

此示例使用列表(数字表)来实现此目的,因为我找到了 那些列表更方便:

CREATE OR REPLACE TYPE numberlist AS TABLE OF NUMBER;

DECLARE 
   v_numberlist numberlist;
BEGIN
   SELECT intval numbercolumn
     BULK COLLECT INTO v_numberlist
     FROM lookup;
   FOR i IN 1..v_numberlist.count
   LOOP
      dbms_output.put_line( v_numberlist(i) );
   END LOOP;     
END;

答案 2 :(得分:1)

您可能还想将整个选择放在表格中。您可以使用BULK COLLECT到数组:

CREATE OR REPLACE TYPE t_my_list AS TABLE OF VARCHAR2(100);    

CREATE OR REPLACE 
PROCEDURE get_tables(p_owner in varchar2)
as
  v_res   t_my_list;
  v_qry   varchar2(4000) := '';
begin

    v_qry :=  ' SELECT table_name from all_tables where owner='''||p_owner||'''';            
    dbms_output.put_line(v_qry);    
    -- all at once in the table
    execute immediate v_qry bulk collect into v_res;
    FOR I in 1..v_res.count 
    loop
        dbms_output.put_line(v_res(i));
    end loop;

exception
when others then
    raise;
end get_tables;
/

begin
    get_tables('E') ;
end;
/

答案 3 :(得分:1)

只是使用某些本机SQL数据类型的选项。希望它有所帮助。

SET SERVEROUTPUT ON;
DECLARE
  lv_num_tab DBMS_SQL.NUMBER_TABLE;
BEGIN
  SELECT LEVEL BULK COLLECT INTO lv_num_tab FROM DUAL CONNECT BY LEVEL < 10;
  FOR I IN lv_num_tab.FIRST..lv_num_tab.LAST
  LOOP
    dbms_output.put_line(lv_num_tab(i));
  END LOOP;
END;