plsql 将游标读取到table中

-- 将游标中的数据 读取到table中   根据部门编号获得emp所有信息。
declare
                                                             
  cursor c(no emp.deptno%type)is select * from emp where deptno=no;   --1.定义游标
 
  type emp_table_type is table of emp%rowtype index by binary_integer;  --2.定义table
  emp_table emp_table_type;                                             --4.使用 table
 
  row1 emp%rowtype;   --3.定义 rowtype
begin
  open c(30);  --输入部门编号
  fetch c bulk collect into emp_table; --批量导入 
  for i in emp_table.first..emp_table.last  -- for 循环便利  从table中的  first 到  last   emp_table.first..emp_table.last
  loop
    dbms_output.put_line(
      'cursor 编号'||i||
      '------员工编号=='||emp_table(i).empno||
      '------员工姓名=='||emp_table(i).ename
    );
  end loop; 
  close c;
end;

二、游标类型的变量   存储过程中当作参数使用;

--4、游标类型的变量   存储过程中当作参数使用;

declare
  type cur_type is ref cursor; --声明一个游标的类型  类似于 声明一个table的数据类型   1.声明   2.使用!
  cur1 cur_type; -- 声明变量  使用变量。  
  row1 emp%rowtype;
begin
  open cur1 for select * from emp;  
  
  loop
    fetch cur1 into row1;
    exit when cur1%notfound;
    dbms_output.put_line(
      '----姓名=='||row1.ename||
      '----工作=='||row1.job
    );
    
  end loop;
  
  close cur1;
end;

三、使用for简化游标的操作

-- 5、使用for简化游标的操作
declare
  cursor c is select * from emp;  -- 定义游标
begin
  
  for row1 in c loop                            --row1 从游标中提取的一行数据  不需要定义直接用的啊? 或者说是 for循环自带定义功能
    dbms_output.put_line(                        --  直接循环 从前到后  不需要进行判断!
      '姓名=='||row1.ename||
      '---工资=='||row1.sal||
      '-- cursor 角标=='||c%rowcount
    );
  end loop;
end;
原文地址:https://www.cnblogs.com/ZXF6/p/11234104.html