Oracle 集合-Learning-1

集合-Test1

  • bulk collect into 批量插入,可用limit 限制插入行数
  • type ... is table of DataType Index by binary_Integer 其中 index by binary_integer 在定义schema级 type 时没有使用,而在session 级别的定义中必须使用
  • extend 空间扩展,使用schema级别的 type 时需要使用extend 申请扩展空间,而回话级别是不需要的的。
  • list1 multiset except list2 从集合 list1中排除 list2并返回差集
declare
emp_type  employee.employeename%type;
type emp_arr_type is table of varchar2(30) index by binary_integer;
emp_arr emp_arr_type;
type cur_type is ref cursor;
cur cur_type;
begin
 open cur for select employeename from employee;
 fetch cur bulk collect into emp_arr;
 for i in 1..emp_arr.count 
 loop 
   dbms_output.put_line(emp_arr(i));
 end loop;
end;
--------------输出结果-----------
王红
黎明
涂洪
王小兵
黄奋
李志强
李立
黄小红
黄勇
王红
黄志超
王加红
章常勇
李红
曾小勇
吴晓红
-----------------------------------

declare 
type emp_name_t is table of employee.employeename%type index by binary_integer;
emp_name emp_name_t;
l_row binary_integer;
begin
 emp_name(2000):='zhangsan';   --回话级别的关联数组直接赋值,无需单独申请扩展空间
 emp_name(-5):='李四';
 emp_name(1):='22';
 l_row:=emp_name.first;
 while (l_row is not null)
 loop
   dbms_output.put_line(emp_name(l_row));
   l_row:=emp_name.next(l_row);
 end loop; 
end;

-------------输出结果--------
李四
22
zhangsan
-----------------------------


create type list_of_name_t is table of varchar2(100);  --声明一个schema级别的类型 list_of_name_t

declare 
  happyfamily list_of_name_t:=list_of_name_t(); --实例化对象
  children list_of_name_t:=list_of_name_t();
  parents list_of_name_t:=list_of_name_t();
begin
  happyfamily.extend(4); --扩展4个空间 
  happyfamily(1):='eli';
  happyfamily(2):='steven';
  happyfamily(3):='chirs';
  happyfamily(4):='veva';
  children.extend;  --使用之前先扩展空间(上面的例子不需要扩展空间是因为类型是回话级别的)
  children(1):='chirs';
  children.extend;
  children(2):='eli';
  parents:=happyfamily multiset except children;  --排除了集合children 
  for l_row in parents.first ..parents.last
    loop
      dbms_output.put_line(parents(l_row));
    end loop;
end;

------------输出结果----------
steven
veva
------------------------------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章