码迷,mamicode.com
首页 > 数据库 > 详细

Oracle 游标的使用

时间:2014-09-22 20:59:33      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:des   http   io   os   使用   java   ar   for   数据   

1.什么是游标?

答:游标是系统给用户开设的一个数据缓冲区,存放SQL语句的执行结果,

  每个游标都有一个一个名字,用户可以用SQL语句从游标中提取数据,然后赋给变量。

2.游标分类

  答:游标分为隐式游标(游标属性)、显示游标(游标技巧)和REF游标(游标变量);

3.隐式游标如何使用?

  答:使用DML时自动创建隐式游标,它是自动声明、自动打开、自动关闭的,名字为SQL.

    属性有:%found,%notfound,%rowcount,%isopen.

    bubuko.com,布布扣

4.如何使用显式游标?

    答:显式游标在PL/SQL块中声明部分定义查询,该查询可以返回一行或多行记录。

      使用显式游标分为四步:

      (1)、声明游标

        declare 

          cursor mycursor is

          select ename,sal from emp where deptno=10;

          v_name emp.ename*type;

          v_sal emp.sal%type;

        begin

      (2)、打开游标

        open mycursor ;

      (3)、使用游标

        loop

        fetch mycursor into v_name,v_sal ;

        exit when mycursor%notfound;

        dbms_output.put_line(v_name||‘->‘||v_sal);

        end loop;

      (4)、关闭游标

        close mycursor;

    end;

5、使用带参数的显式游标。

  declare cursor emp_cursor(dno number) is

    select ename,sal from emp

    where deptno=dno

    emp_record emp_cursor%rowtype;

  begin

    IF not emp_cursor%isopen then

    open emp_cursor(20);

    END IF;

  loop

    fetch emp_cursor into emp_record

    exit when emp_cursor%notfound

    dbms_output.put_line(emp_record.ename||‘->‘||emp_record.sal);

  end loop;

  close emp_cursor;

  end;

 

6、循环游标(不用打开和关闭游标但需要声明一个与游标相同类型的游标变量)

  declare cursor emp_cursor(dno number) is

    select ename,sal from emp

    where deptno=dno;

    emp_record emp_cursor%rowtype;

  begin

   for emp_record in emp_cursor(10) loop

    dbms_output.putline(v_name||‘->‘||v_sal);

    end loop;

  end;

7、使用游标更新行

  declare

    cursor emp_cursor is

      select ename,sal,deptno from emp for update;--锁定emp表,执行完后不提交或者回滚将会把当前表锁定

    emp_record emp_cursor%rowtype;

    begin

      if not emp_cursor%isopen then

        open emp_cursor;

      end if;

    loop

      fetch emp_cursor into emp_record;

      exit when emp_cursor%notfound;

      if emp_record.deptno = 30 then

        update emp set sal=sal+100 where current of emp_cursor;

      end if;

     dbms_output.put_line(emp_record.sal);

    end loop;

    close emp_cursor;

  end;

 

8、ref游标(游标变量)

  declare

    type my_type is ref cursor;

    cv my_type;

    --cv sys_refcursor;

    v_lastname employee.ename%type;

    query_2 varchar2(200):=‘select * from dept‘;

    v_emp emp%rowtype;

    v_dept dept%rowtype;

  begin

    open cv for

      select ename from emp

      where job=‘MANAGER‘

      order by ename;

    loop

      fetch cv into v_lastname;

      exit when cv%notfound;

      dbms_output.put_line(v_lastname);

    end loop;

    dbms_output.put_line(‘----------------‘);

    open cv for query_2;

    loop

      fetch cv into v_dept;

      exit when cv%notfound;

      dbms_output.put_line(v_dept.dname);

    end loop;

    close cv;

  end;

Oracle 游标的使用

标签:des   http   io   os   使用   java   ar   for   数据   

原文地址:http://www.cnblogs.com/wdlove/p/3986389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!