Oracle中数组类型的几种用法

来源:互联网 发布:ntfs for mac安装包 编辑:程序博客网 时间:2024/05/29 07:41

这里只讨论简单讨论一下Oracle中嵌套表数组的用法: 

declare
  type my_first_type is table of varchar2(20) index by binary_integer;
  l_test_type my_first_type;
  l_index number ;
begin
  for t_id_no in (select * from ls_appl) loop
    l_test_type(t_id_no.ls_appl_seq):=t_id_no.appl_loan_no;
  end loop;
 
  l_index := l_test_type.first;
  loop
    exit when l_index is null ;
    dbms_output.put_line(l_index ||' : ' ||l_test_type(l_index));
    l_index := l_test_type.next(l_index);
  end loop;
end;

上面的用法中声明一个varchar2类型的数组,其实of后面的只是一个数据类型。

我们用ls_appl表中的一个字段去填充它。

declare
  type my_type is table of loan%rowtype index by binary_integer;
  l_my_type my_type;
begin
  select * bulk collect into l_my_type from loan;
  for l_c in 1 .. l_my_type.count loop
    dbms_output.put_line(l_my_type(l_c).loan_no ||' : '||l_my_type(l_c).ctif_id_no);
  end loop;
end;

上面的用法有些时候可以用于替代cursor。

declare
  type my_record is record(hh number(15),nn varchar2(20));
  l_my_record my_record;
  type my_type is table of l_my_record%type index by binary_integer;
  l_my_type my_type;
begin
  select l.ls_appl_seq,l.appl_no bulk collect into l_my_type from ls_appl l;
  for i in 1 .. l_my_type.count loop
    dbms_output.put_line(l_my_type(i).hh || ' : ' ||l_my_type(i).nn);
  end loop;
end;

上面的用法中自己先定义了一个类型,然后又定义了该类型的数组。

 

参考:http://fxz-2008.iteye.com/blog/469766