Oracle中IS TABLE OF的使用

IS TABLE OF :指定是一个集合的表的数组类型,简单的来说就是一个可以存储一列多行的数据类型。

INDEX BY BINARY_INTEGER:指索引组织类型

BULK COLLECT :指是一个成批聚合类型,简单的来说 , 它可以存储一个多行多列存储类型,采用BULK COLLECT可以将查询结果一次性地加载到集合中。

【实例】在SCOTT模式下,使用IS TABLE OF获取所有员工的姓名,职务,工资信息。

declare  
  type type_ename is table of emp.ename%type;     
  type type_job is table of emp.job%type;  
  type type_sal is table of emp.sal%type;  
    
  var_ename type_ename:=type_ename();  
  var_job type_job:=type_job();  
  var_sal type_sal:=type_sal();  
begin  
  select ename,job,sal   
  bulk collect into var_ename,var_job,var_sal  
  from emp;  
    
  /*输出雇员信息*/  
  for  v_index in var_ename.first .. var_ename.last loop  
      dbms_output.put_line('雇员名称:'||var_ename(v_index)||' 职务:'||var_job(v_index)||' 工资:'||var_sal(v_index));  
  end loop;  
end; 

【实例】在SCOTT模式下,使用IS TABLE OF获取所有员工的所有信息。

    declare  
       type emp_table_type is table of emp%rowtype index by binary_integer;  
       var_emp_table emp_table_type;  
    begin  
      select *  
      bulk collect into var_emp_table  
      from emp;  
        
      /*输出雇员信息*/  
      for i in 1..var_emp_table.COUNT loop  
          dbms_output.put_line('雇员名称:'||var_emp_table(i).ename||' 职务:'||var_emp_table(i).job||' 工资:'||var_emp_table(i).sal);  
      end loop;  
    end;  

猜你喜欢

转载自www.cnblogs.com/xwb583312435/p/9056029.html