oracle 游标

经常用
create or replace PROCEDURE PROC_STU1_1 AS
BEGIN
  --显示游标使用,使用exit when循环 
  declare
    --1.定义游标,名称为cur_stu 
    cursor cur_stu is
      select empno, ename from emp;
    --定义变量,存放游标取出的数据 
    v_stuno   varchar(4);
    v_stuname varchar(20);
  begin
    --2.打开游标cur_stu 
    open cur_stu;
    loop
      --3.将游标的当前行取出存放到变量中 
      fetch cur_stu
        into v_stuno, v_stuname;
      exit when cur_stu%notfound; --游标所指还有数据行,则继续循环 
      --打印结果 
      dbms_output.PUT_LINE(v_stuno || '->' || v_stuname);
    end loop;
    close cur_stu; --4.关闭游标 
  end;
END PROC_STU1_1;
===================================================================
create or replace procedure testLoop3 as
  --显示游标 使用while 
begin
  declare
    --定义游标
    cursor myCursor is
      select ename, sal from emp;
    --定义变量
    e_name varchar(20);
    e_sal  number(20);
  begin
    --打开游标
    open myCursor;
    --游标所指行还有数据
    while myCursor%found loop
      dbms_output.put_line(e_name || '============' || e_sal);
      --fetch 表示 将游标当前指向的值复制给变量 然后移到下一个
      --将取出的值放在变量中
      fetch myCursor
        into e_name, e_sal;
    end loop;
    --关闭游标
    close myCursor;
  end;
end;
 
=================================================
create or replace procedure testCursor is
  --显示游标使用if 判断 
begin
  declare
    --定义游标
    cursor myCursor is
      select empno, ename from emp order by empno;
    --定义变量
    e_no   int;
    e_name varchar(20);
  begin
    --打开游标
    open myCursor;
    --if判断 
    loop
      if myCursor%found then
        dbms_output.put_line(e_no || '--->' || e_name);
        --将游标当前值复制给变量
        fetch myCursor
          into e_no, e_name;
      end if;
     end loop;
    close myCursor;
  end;
end;
 
=======================================================
create or replace procedure testCursor2 is
begin
  declare
    cursor myCur is
      select empno, ename from emp order by empno;
--不用声明变量   
begin
    for emp in myCur loop
      dbms_output.put_line(emp.empno || '--->' || emp.ename);
    end loop;
--自动关闭游标  
  end;
end testCursor2;
===========================================================================、
CREATE OR REPLACE PROCEDURE PROC_STU4 AS
BEGIN
  --隐式游标使用 
  update etoak set name = '张燕广' where id = '11';
END PROC_STU4;
posted @ 2012-09-14 19:56  Just_Begin  阅读(264)  评论(0编辑  收藏  举报