ITPub博客

首页 > Linux操作系统 > Linux操作系统 > pl/sql游标的使用

pl/sql游标的使用

原创 Linux操作系统 作者:hit-2075 时间:2011-11-15 16:52:52 0 删除 编辑
---------------------------显示游标
declare
--申明游标
  cursor c_emp_cursor is select serialno,no from temp_dnpath ;
  v_no temp_dnpath.no%type;
  v_serialno temp_dnpath.serialno%type;
begin
--打开游标
  open c_emp_cursor;
  loop
--获取数据
    fetch c_emp_cursor into v_serialno,v_no;
--循环判断退出
    exit when c_emp_cursor%notfound;
--操作
    dbms_output.put_line(v_no || ' ' || v_serialno);
  end loop;
--关闭游标
  close c_emp_cursor;
end;
----------------------------显示游标配合记录使用
declare
    cursor e is select * from temp_dnpath;
--定义游标类型的记录
    pathrec e%rowtype;
begin
  open e;
  loop
--直接放到记录里
    fetch e into pathrec;
    exit when e%notfound;
    dbms_output.put_line('no is => ' || pathrec.no || ' and serialno is => ' || pathrec.serialno );
  end loop;
  close e;
end;
---------------------------隐示游标
declare
--申明游标
  cursor e is select * from temp_dnpath;
begin
--游标直接付给i,其他的不用管了
  for i in e
    loop
--用i去操作
      dbms_output.put_line('serialno ==> ' || i.serialno);
      dbms_output.put_line('rowcount ==> ' || e%rowcount);
    end loop;
end;
---------------------------
begin
--游标的申明都省去料
  for i in (select * from temp_dnpath)
  loop
    dbms_output.put_line(i.no || ' --> ' || i.serialno );
  end loop;
end;
---------------------------带参数的游标使用
declare
--申明游标,参数在where子句中
  cursor e (l_no number) is select * from temp_dnpath where no = l_no;
  r e%rowtype;
begin
--参数变化,游标内容也变化
  for i in e(10)
  loop
    dbms_output.put_line(i.no || ' --> ' || i.serialno);
  end loop;
 
  dbms_output.put_line('----------------------------->');
 
  for i in e(5)
  loop
    dbms_output.put_line(i.no || ' --> ' || i.serialno);
  end loop; 
 
  dbms_output.put_line('----------------------------->');
--显示使用 
  open e(9);
  loop
    fetch e into r;
    exit when e%notfound;
    dbms_output.put_line(r.no || ' --> ' || r.serialno); 
  end loop;
  close e;
 
end;
--------------------------bulk collect很霸气,需要配合关联数组
declare
  type path_type is table of temp_dnpath%rowtype index by pls_integer;
  l_path path_type;
  l_row  pls_integer;
begin
--一条语句全部取到关联数组中去
  select * bulk collect into l_path from temp_dnpath;
--慢慢使用,数组长度
  dbms_output.put_line('the count is : ' || l_path.count);
--遍历输出 
  l_row := l_path.first;
  while(l_row is not null)
  loop
    dbms_output.put_line(l_row ||': ' || l_path(l_row).no || '--->' || l_path(l_row).serialno );
    l_row := l_path.next(l_row);
  end loop;
end;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15681893/viewspace-710992/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: PL/SQL使用记录
下一篇: pl/sql异常处理
请登录后发表评论 登录
全部评论

注册时间:2011-09-18

  • 博文量
    28
  • 访问量
    44381