oracle 游标

PL/SQL代码块中只能用select语句进行赋值 select…into…from….,不能查询一个结果集 
(如需用表请将鼠标移到底部,有创建表代码) 
(1).隐式游标

begin
     dbms_output.put_line('执行之前,影响的行数:'||SQL%ROWCOUNT);
     delete from orderinfo where orderid=20160002;
     dbms_output.put_line('执行之后,影响的行数:'||SQL%ROWCOUNT);
end;

(2).显示游标:定义游标时指定查询语句

     CURSOR  游标名  IS  操作语句;   --1、定义游标 

     OPEN   游标名;  --2、打开游标
     LOOP
       FETCH   游标名   INTO    变量名;  --3.读取游标
             EXIT  WHEN   游标名%NOTFOUND;
             处理语句;
      END   LOOP;
      CLOSE    游标名;--4.关闭游标

    create or replace procedure proc1
    as
      cursor cur_user is select * from userinfo; --1.定义游标
      theUser userinfo%rowtype; --定义变量与userinfo数据行映射,用于存储游标读取到的数据 
    begin
      --2.打开游标
      open cur_user;
      LOOP
        --3.获取游标的下一行数据
        FETCH CUR_USER INTO theUser;
        --游标没有读到数据退出循环
        exit when CUR_USER%notfound; 
        ---游标读到数据就输出
        dbms_output.put_line(theUser.userId||'  '||theUser.userName);
      END LOOP; 
      dbms_output.put_line('数据备份完毕!');
      --4.关闭游标
     close cur_user;
     end;

示例2:

create or replace procedure proc1(upoint int)
as
  cursor cur_user is select * from userinfo where userPoint>upoint; --1.定义游标
  theUser userinfo%rowtype; --定义变量与userinfo数据行映射,用于存储游标读取到的数据 
begin
  --2.打开游标
  open cur_user;
  LOOP
    --3.获取游标的下一行数据
    FETCH CUR_USER INTO theUser;
    --游标没有读到数据退出循环
    exit when CUR_USER%notfound; 
    ---游标读到数据就输出
    dbms_output.put_line(theUser.userId||'  '||theUser.userName);
  END LOOP; 
  dbms_output.put_line('数据备份完毕!');
  --4.关闭游标
 close cur_user;
end;
call proc1(0);

示例3:带参数的游标

create or replace procedure proc1
as
  cursor cur_user(upoint int) is select * from userinfo where userPoint>upoint; --1.定义游标
  theUser userinfo%rowtype; --定义变量与userinfo数据行映射,用于存储游标读取到的数据 
begin
  --2.打开游标
  open cur_user(2);
  LOOP
    --3.获取游标的下一行数据
    FETCH CUR_USER INTO theUser;
    --游标没有读到数据退出循环
    exit when CUR_USER%notfound; 
    ---游标读到数据就输出
    dbms_output.put_line(theUser.userId||'  '||theUser.userName);
  END LOOP; 
  dbms_output.put_line('数据备份完毕!');
  --4.关闭游标
 close cur_user;
end;
call proc1();

示例4:for循环读取游标

create or replace procedure proc1
as
  cursor cur_user is select * from userinfo; --1.定义游标
begin

   for theUser in cur_user
   loop
       dbms_output.put_line(theUser.userId||'  '||theUser.userName);
   end loop;
  dbms_output.put_line('数据备份完毕!');
end;

call proc1();

4.引用游标:定义时不指定查询语句,打开时指定查询语句

    TYPE    游标类型名   IS  REF   CURSOE; --定义类型
    游标变量名  游标类型名;  --使用类型定义游标变量
    OPNE   游标变量名   FOR   查询语句;
create or replace procedure proc1(i int)
as
  type refType is ref cursor;--定义类型 ,类型名为refType
  theCursor refType;--通过类型定义游标 游标名为theCursor
  theUser userInfo%rowtype;
  thePro proInfo%rowtype; 
begin
   if i=1 then
      open theCursor for select * from userInfo;
      loop
          fetch theCursor into theUser;
          exit when theCursor%notfound;
          dbms_output.put_line(theUser.userId||'  '||theUser.userName);
      end loop;
      close theCursor;
   elsif i=2 then
      open theCursor for select * from proInfo;
      loop
          fetch theCursor into thePro;
          exit when theCursor%notfound;
          dbms_output.put_line(thePro.proId||'  '||thePro.proName);
      end loop;
      close theCursor;
   else
     dbms_output.put_line('选择错误!');  
   end if;  
end proc1;
call proc1(2);
原文地址:https://www.cnblogs.com/bug1024/p/8962117.html