declare
   type i_type is table of varchar2(50) index by varchar2(50);
   tab i_type;
   idx varchar2(50);
 begin
   tab('A'):='东邪';
   tab('a'):='西毒';
   tab('g'):='南帝';
   tab('d'):='北丐';
   tab('p'):='中神通';
   idx := tab.first;
   loop
     dbms_output.put_line(tab(idx));
     exit when idx=tab.last;
     idx:=tab.next(idx);
   end loop;
 end;
----------------------------------------------------------------------------
 declare
   type i_tab is table of varchar2(50) 
 index by varchar2(50);
   tab i_tab;
   i varchar2(50);
 begin
   tab('a'):='东邪';
   tab('b'):='西毒';
   tab('c'):='南帝';
   tab('d'):='北丐';
   tab('e'):='中神通';
   i:=tab.first;
   <<A>>
   dbms_output.put_line(tab(i));
   i:=tab.next(i);
   if i is not null then
     goto A;
   end if;
 end;
----------------------------------------------------------------------------
declare
    --声名一个索引表类型
    type mytype is table of varchar2(200) index by varchar2(5);
    --声名一个索引表类型变量
    tab mytype;
    --声名一个变量保存索引表的下标
    ind varchar2(5);
 begin
   tab('d'):='东邪';
   tab('e'):='西毒';
   tab('b'):='南帝';
   tab('a'):='北丐';
   tab('f'):='中神通';
   ind:=tab.first;
    ---遍历集合
    while ascii(ind)<=ascii(tab.last) loop
       --打印集合元素
       dbms_output.put_line(tab(ind));
       ind:=tab.next(ind);--ind:=ind+1;
    end loop;
 end;
----------------------------------------------------------------------------
--4.取出所有员工的薪资(sal),存入嵌套表中,通过遍历集合得出总工资和:
 declare
    type a_type is table of emp%rowtype ;
    e_tab a_type := a_type();
    x number := 1;
    all_sal number := 0;
 begin
   for i in (select * from emp) loop
     e_tab.extend;
     e_tab(x) := i;
     x := x+1;
   end loop;
   for j in 1..e_tab.count loop
     all_sal := all_sal + e_tab(j).sal;
   end loop;
   dbms_output.put_line('总工资为:'||all_sal);
 end;
--5.创建一个与emp字段相同的空表copy_emp,
 --利用集合把emp表中的数据导入copy_emp:
-- create or replace type copy_type is table of emp_type;
 create table copy_emp
 as (select * from emp where 1=0);
declare
   type b_type is table of emp%rowtype;
   e_tab b_type := b_type();
   x number := 1;
 begin
   for i in (select * from emp) loop
     e_tab.extend;
     e_tab(x) := i;
     x := x+1;
   end loop;
   for j in 1..e_tab.count loop
     insert into copy_emp values(e_tab(j).empno,e_tab(j).ename,
     e_tab(j).job,e_tab(j).mgr,e_tab(j).hiredate,e_tab(j).sal,
     e_tab(j).comm,e_tab(j).deptno);
   end loop;
 end;
 select * from copy_emp;
 --6.按部门编号将员工存入不同的集合,再取出打印:
 declare
   type c_type is table of emp%rowtype;
   e_tab c_type := c_type();
   x number := 1;
   dno number := &部门号;
 begin
   for i in (select * from emp where deptno = dno) loop
     e_tab.extend;
     e_tab(x) := i;
     x := x+1;
   end loop;
   for j in 1..e_tab.count loop
     dbms_output.put_line(e_tab(j).empno||e_tab(j).ename||'  '||
     e_tab(j).job||'  '||e_tab(j).mgr||'  '||e_tab(j).hiredate
     ||'  '||e_tab(j).sal||'  '||
     e_tab(j).comm||'  '||e_tab(j).deptno);
   end loop;
 end;
--
 declare
   type d_type is table of emp%rowtype;
   e_10_tab d_type := d_type();
   e_20_tab d_type := d_type();
   e_30_tab d_type := d_type();
   x number := 1;
   y number := 1;
   z number := 1;
 begin
   for i in (select * from emp where deptno = 10) loop
     e_10_tab.extend;
     e_10_tab(x):=i;
     x:=x+1;
   end loop;
   for i in (select * from emp where deptno = 20) loop
     e_20_tab.extend;
     e_20_tab(y):=i;
     y:=y+1;
   end loop;
   for i in (select * from emp where deptno = 30) loop
     e_30_tab.extend;
     e_30_tab(z):=i;
     z:=z+1;
   end loop;
   for j in 1..e_10_tab.count loop
     dbms_output.put_line(e_10_tab(j).empno||e_10_tab(j).ename||'  '||
     e_10_tab(j).job||'  '||e_10_tab(j).mgr||'  '||e_10_tab(j).hiredate
     ||'  '||e_10_tab(j).sal||'  '||e_10_tab(j).comm||'  '||e_10_tab(j).deptno);
   end loop;
   for j in 1..e_20_tab.count loop
     dbms_output.put_line(e_20_tab(j).empno||e_20_tab(j).ename||'  '||
     e_20_tab(j).job||'  '||e_20_tab(j).mgr||'  '||e_20_tab(j).hiredate
     ||'  '||e_20_tab(j).sal||'  '||e_20_tab(j).comm||'  '||e_20_tab(j).deptno);
   end loop;
   for j in 1..e_30_tab.count loop
     dbms_output.put_line(e_30_tab(j).empno||e_30_tab(j).ename||'  '||
     e_30_tab(j).job||'  '||e_30_tab(j).mgr||'  '||e_30_tab(j).hiredate
     ||'  '||e_30_tab(j).sal||'  '||    e_30_tab(j).comm||'  '||e_30_tab(j).deptno);
   end loop;
 end;
--7.将所有员工信息保存到一个集合中,然后删除下标为3,5,7,及最后三个下标对应的元素。
 -- 然后给下标为5的元素重新赋值,最后输出集合中元素的个数:
 declare
   type e_type is table of emp%rowtype;
   e_tab e_type := e_type();
   x number := 1;
   cnt number := 0;
 begin
   for i in (select * from emp) loop
     e_tab.extend;
     e_tab(x) := i;
     x := x+1;
   end loop;
   cnt := e_tab.count;
   for j in 1..e_tab.count loop
     if j in (3,5,7,cnt,cnt-1,cnt-2) then
       e_tab.delete(j);
     end if;
   end loop;
   e_tab(5) := null; 
   dbms_output.put_line(e_tab.count);
 end;
 select * from emp;
--8.编写一个点名器,声明一个集合,存储五个同学的名字,
 -- 每次执行后会随机输出一个名字,并且输出后,
 --该名字在下次执行时不会出现(生成1-5随机整数:trunc(dbms_random.value(1,6))):
create table sttuu(sname varchar2(50));
 insert into sttuu values('东邪');
 insert into sttuu values('西毒');
 insert into sttuu values('南帝');
 insert into sttuu values('北丐');
 insert into sttuu values('中神通');
truncate table sttuu;
 select * from sttuu;
 declare
   type stu_type is table of sttuu%rowtype;
   s_tab stu_type := stu_type();
   cursor cur is select * from sttuu;
   x number := 1;
   n number;
   i number := 5;
 begin
   select count(*) into i from sttuu;
   s_tab.extend(i);
   for s_cur in cur loop
     s_tab(x).sname := s_cur.sname;
     x := x+1;
   end loop;
   if i > 0 then
   n := trunc(dbms_random.value(1,i));
   dbms_output.put_line(s_tab(n).sname); 
   delete from sttuu where sname = s_tab(n).sname;     
   s_tab.delete(n); 
   else  
     dbms_output.put_line('NO DATA!');
   end if;
 end;
 ----
 declare
    type stu_type is record(
      sname varchar(50)    
    );
    type s_type is table of stu_type;
    stu s_type := s_type();
    nid number ;
    x number;
 begin
   stu.extend(5);
   stu(1).sname := '东邪'; 
   stu(2).sname := '西毒';
   stu(3).sname := '南帝';
   stu(4).sname := '北丐';
   stu(5).sname := '中神通';
   x := stu.count;
   <<A>>
   while x>0 loop
     nid := trunc(dbms_random.value(1,6)); 
     if stu.exists(nid) then
       dbms_output.put_line(stu(nid).sname);
       stu.delete(nid); 
       x:=x-1;
     end if;
     goto A;
   end loop;
 end;
 create table sttuu(sname varchar2(50));
 insert into sttuu values('东邪');
 insert into sttuu values('西毒');
 insert into sttuu values('南帝');
 insert into sttuu values('北丐');
 insert into sttuu values('中神通');
truncate table sttuu;
 select * from sttuu;
 declare
   type stu_type is table of sttuu%rowtype;
   s_tab stu_type := stu_type();
   cursor cur is select * from sttuu;
   x number := 1;
   n number;
   i number := 5;
 begin
   select count(*) into i from sttuu;
   s_tab.extend(i);
   for s_cur in cur loop
     s_tab(x).sname := s_cur.sname;
     x := x+1;
   end loop;
   if i > 1 then
   n := trunc(dbms_random.value(1,i));
   dbms_output.put_line(s_tab(n).sname); 
   delete from sttuu where sname = s_tab(n).sname;     
   s_tab.delete(n); 
   else 
     dbms_output.put_line(s_tab(1).sname); 
     delete from sttuu where sname = s_tab(1).sname;     
     s_tab.delete(1); 
   end if;
 end;