游标(Cursor):用来查询数据库,获取记录集合(结果集)的指针,可以让开发者一次访问一行结果集,在每条结果集上作操作。

游标可分为:

1.       静态游标:分为显式(explicit)游标和隐式(implicit)游标。

2.       REF游标:是一种引用类型,类似于指针。

1、静态游标

1.1显式游标

定义格式:

CURSOR 游标名 ( 参数 )  IS

Select 语句 FOR UPDATE [OF [schema.]table.column[,[schema.]table.column]..

[nowait]

例子1 :无参数,打开关闭游标

set serveroutput on size 10000000 ;

create or replace procedure TEST is
  cursor c1 is
    select tname from tab;
  pname varchar2(32);
begin
  open c1;
  loop
    fetch c1
      into pname;
    exit when c1%notfound;
    dbms_output.put_line(pname);
  end loop;
  close c1;

end TEST

exec test;

例子2 :参数使用,参数使用方法和存储过程一样

create or replace procedure TEST is
  cursor c1(pname in varchar2is
    select tname from tab where tname like pname;
  pname varchar2(32);
begin
  open c1('T%');
  loop
    fetch c1
      into pname;
    exit when c1%notfound;
    dbms_output.put_line(pname);
  end loop;
  close c1;

end TEST;

1.2隐式游标

不用明确建立游标变量,分两种:

1.在PL/SQL中使用DML语言,使用ORACLE提供的名为“SQL”的隐示游标。

举例:

declare
begin
  update departments set department_name = department_name;
  --where 1=2;
  dbms_output.put_line('update ' || sql%rowcount || ' records');
end;
/

2.CURSOR FOR LOOP,用于for loop 语句

举例:

例子1:无参数,使用循环,无须打开关闭,本人这种方式

create or replace procedure TEST is
  cursor c1 is
    select tname from tab;
begin
  for rr in c1 loop
    dbms_output.put_line(rr.tname);
  end loop;

end TEST;

例子1:有参数,使用循环,无须打开关闭,本人这种方式

create or replace procedure TEST is
  cursor c1(pname in varchar2is
    select tname from tab where tname like pname;
begin
  for rr in c1('T%') loop
    dbms_output.put_line(rr.tname);
  end loop;

end TEST;

1.3游标常用属性:

%FOUND:变量最后从游标中获取记录的时候,在结果集中找到了记录。

%NOTFOUND:变量最后从游标中获取记录的时候,在结果集中没有找到记录。

%ROWCOUNT:当前时刻已经从游标中获取的记录数量。

%ISOPEN:是否打开。

Declare  /* /* 定义静态游标 */ */
  Cursor emps is
    Select * from employees where rownum < 6 order by 1;

Emp employees%rowtype;
  Row number := 1;
Begin
  Open emps; /* ´打开静态游标 */
  Fetch emps
    into emp; /*  读取游标当前行  */

Loop
    If emps%found then
      Dbms_output.put_line('Looping over record ' || row || ' of ' ||
                           emps%rowcount);
      Fetch emps
        into emp;
      Row := row + 1;
    Elsif emps%notfound then
      Exit;
    End if;
  End loop;

If emps%isopen then
    Close emps; /*  关闭游标  */
  End if;
End;
/

1.4 游标的更新和删除

在PL/SQL中依然可以使用UPDATE和DELETE语句更新或删除数据行。显式游标只有在需要获得多行数据的情况下使用。PL/SQL提供了仅仅使用游标就可以执行删除或更新记录的方法。

UPDATE或DELETE语句中的WHERE CURRENT OF子串专门处理要执行UPDATE或DELETE操作的表中取出的最近的数据。要使用这个方法,在声明游标时必须使用FOR UPDATE子串,当对话使用FOR UPDATE子串打开一个游标时,所有返回集中的数据行都将处于行级(ROW-LEVEL)独占式锁定,其他对象只能查询这些数据行,不能进行UPDATE、DELETE或SELECT...FOR UPDATE操作。

在多表查询中,使用OF子句来锁定特定的表,如果忽略了OF子句,那么所有表中选择的数据行都将被锁定。如果这些数据行已经被其他会话锁定,那么正常情况下ORACLE将等待,直到数据行解锁。

在UPDATE和DELETE中使用WHERE CURRENT OF子串的语法如下:

WHERE{CURRENT OF cursor_name|search_condition}

例:

create or replace procedure pc_SetVersionValid(PFlowsID in integeris
  Cursor c1 is
    select *
      from wf_flows
     where flowname in
           (select flowname from wf_flows where flowsid = PFlowsID)
       for update;

r c1%rowtype;
  v integer;
begin
  open c1;
  fetch c1
    into r;
  while c1%found loop
    if r.flowsid = PFlowsID then
      v := 1;
    else
      v := 0;
    end if;
  
    UPDATE wf_flows SET isValid = v WHERE CURRENT OF c1;
  
    fetch c1
      into r;
  
  end loop;
  close c1;
  commit;
end;

显式和隐式游标的区别:

尽量使用隐式游标,避免编写附加的游标控制代码(声明,打开,获取,关闭),也不需要声明变量来保存从游标中获取的数据。

2、REF CURSOR游标

动态游标,在运行的时候才能确定游标使用的查询。可以分为:

create or replace procedure TEST is
  sqlstr varchar2(500);
  type RefCur is ref cursor;
  c1 refcur;
begin
  sqlstr := 'select * from tab';
  open c1 for sqlstr;
  close c1;
end;

REF CURSOR实现BULK功能

1. 可以加速INSERT, UPDATE, DELETE语句的执行,也就是用FORALL语句来替代循环语句。

2. 加速SELECT,用BULK COLLECT INTO 来替代INTO。

SQL> create table tab2  as select empno ID, ename NAME, sal SALARY from emp where 1=2;

create or replace procedure REF_BULK is

/*  定义复杂类型 */

type empcurtyp  is ref cursor;

type idlist  is table of emp.empno%type;

type namelist  is table of emp.ename%type;

type sallist  is table of emp.sal%type;

/* 定义变量  */

emp_cv  empcurtyp;

ids  idlist;

names namelist;

sals sallist;

row_cnt number;

begin

open emp_cv for select empno, ename, sal from emp;

fetch emp_cv  BULK COLLECT  INTO ids, names, sals;

--将字段成批放入变量中,此时变量是一个集合

close emp_cv;

for i in ids.first .. ids.last loop

dbms_output.put_line(' || ids(i) || ' || names(i) ||' salary=' || sals(i));

end loop;

FORALL  i  IN  ids.first .. ids.last

insert into tab2 values (ids(i), names(i), sals(i));

commit;

select count(*) into row_cnt from tab2;

dbms_output.put_line('-----------------------------------');

dbms_output.put_line('The row number of tab2 is ' || row_cnt);

end REF_BULK;

3cursor  ref cursor的区别

从技术底层看,两者是相同的。普通plsql cursor在定义时是“静态”的。而

Ref cursors可以动态打开。

例如下面例子:

Declare

type rc is ref cursor;

cursor c is select * from dual;

l_cursor rc;

begin

if ( to_char(sysdate,'dd') = 30 ) then

open l_cursor for 'select * from emp';

elsif ( to_char(sysdate,'dd') = 29 ) then

open l_cursor for select * from dept;

else

open l_cursor for select * from dual;

end if;

open c;

end;

/

rc根据逻辑动态打开;而游标c定义好了只有就无法修改了。

ref cursor可以返回给客户端,cursor则不行。

cursor可以是全局的global ,ref cursor则必须定义在过程或函数中。

ref cursor可以在子程序间传递,cursor则不行。

cursor中定义的静态sql比ref cursor效率高,所以ref cursor通常用在:向客户端返回结果集。

最新文章

  1. 百度地图API简单应用
  2. C#求任意范围内的质数
  3. python urllib2 发起http请求post
  4. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【三】——Web Api入门
  5. hdu 4026 2011上海赛区网络赛F TSP ****
  6. hdu 4000 Fruit Ninja 树状数组
  7. 极限编程,最强N皇后JAVA解题代码,4秒出15皇后,33秒出16皇后
  8. eclipse 开发技巧
  9. c# 编程添加控件
  10. Controller 中Action 返回值类型 及其 页面跳转的用法
  11. Linux中的ln
  12. hive字符串函数
  13. [Luogu2073]送花
  14. CentOS系统版本的查看方法
  15. 关于pandas 调用mongodb出Memory error错误
  16. node,npm,vue的全局升级
  17. [转] 使用gc &amp;&amp; objgraph 优化python内存
  18. Selenium + WebDriver 各浏览器驱动下载地址
  19. 目标检测--SSD: Single Shot MultiBox Detector(2015)
  20. plupload 大文件分片上传与PHP分片合并探索

热门文章

  1. IJ:目录
  2. Bootstrap中的各种下拉菜单
  3. word excel 等导出相关操作
  4. Ubuntu 12.04 root默认密码? 如何使用root登录? (转载)
  5. POJ3233:Matrix Power Series(矩阵快速幂+递推式)
  6. 51nod 1120 机器人走方格 V3 【卡特兰数+卢卡斯定理+组合数】
  7. JS 数据类型入门与typeof操作符
  8. 安卓Activity全屏显示以及不显示title
  9. OpenCV+VS开发环境配置
  10. [ZPG TEST 116] 最小边权和【生成树相关】