oracle游标实例


--游标
  ----游标:当在PL/SQL块中执行查询语句和数据操作语句时,oracle会为其分配上下文区,游标是指向上下文区的指针。
  ----显示游标:显示游标在PL/SQL块的声明部分声明,在执行部分或异常处理部分打开游标,提取数据,关闭游标。
  ----使用游标不得步骤:
     a.定义游标
     declare cursor cursor_name(游标名) is select_statement(select语句);
     
     b.打开游标
     open cursor_name
    
     c.提取数据
     fetch cursor_name into 参数1,参数2……
     或
     fetch cursor_name bulk collection into collect1,collect2,……
     
     d.关闭游标
     close cursor_name;
    --每次提取一行数据
    declare 
          cursor emp_cursor
    is
          select ename,sal from scott.emp where deptno=20;
          v_ename scott.emp.ename%type;
          v_sal scott.emp.sal%type;
    begin
          --打开游标
          open emp_cursor;
          loop
            fetch emp_cursor into v_ename,v_sal;
            exit when emp_cursor%notfound;
            dbms_output.put_line(v_ename||':'||v_sal);
          end loop;
          --关闭游标
          close emp_cursor;
    end;




      ---每次提取多行数据
      declare 
       cursor emp_cur
       is
       select ename,sal from scott.emp where deptno=20;
              type v_names is table of scott.emp.ename%type
                   index by binary_integer;

              type v_sals is table of scott.emp.sal%type
                   index by binary_integer;

              names v_names;
              sals v_sals;
       begin
         open emp_cur;
         fetch emp_cur bulk collect into names,sals;
         for i in 1..names.count loop
           dbms_output.put_line(sals(i));
           end loop;
         close emp_cur;

       end;


 ----参数游标
 
 declare
       cursor emp_cursor(cno number)
 is
       select ename,sal from scott.emp where deptno=cno;
       v_ename scott.emp.ename%type;
       v_sal scott.emp.sal%type;
 begin
   if not emp_cursor%isopen then
     open emp_cursor(10);---传入参数
   end if;

   loop
     fetch emp_cursor into v_ename,v_sal;
     exit when emp_cursor%notfound;
     dbms_output.put_line(v_ename||':'||v_sal);
   end loop;
   close emp_cursor;
 end;
 
 
 ----使用游标更新或删除数据


   declare 
         cursor emp_cursor
    is
         select ename,sal from scott.emp for update of sal;
          v_ename scott.emp.ename%type;
          v_oldsal scott.emp.sal%type;
    begin
      open emp_cursor;
        loop
          --从游标中提取数据
          fetch emp_cursor into v_ename,v_oldsal;
        exit when emp_cursor%notfound;
        --判断
        if v_oldsal<2500 then
          update scott.emp set sal=sal+150 where current of emp_cursor;
        end if;

        end loop;
      close emp_cursor;
    end;
    
    --查询薪水
    select * from scott.emp;
    
    
    --游标的for循环
    declare 
           cursor emp_cursor
    is
           select ename from scott.emp where deptno=20;
    begin
      for emp_record in emp_cursor loop
        dbms_output.put_line('第'||emp_cursor%rowcount||'个员工'||emp_record.ename);
        end loop;
    end;

 

      --显示游标属性

       %isopen 判断游标是否打开,如果游标已经打开,则返回true,否则返回false;

       %found  检查是否从结果集中提取了数据。如果提取返回true,否则返回false;

       %not found   该属性与%found相反;

       rowcount   返回到当前行数止已经提取到的实际行数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值