oracle cursor游标总结

本文详细介绍了数据库游标的概念、分类及应用。包括静态游标(显式与隐式)、REF游标,并对比了它们的特点和使用场景。同时,还探讨了游标的属性、更新和删除操作,以及REF游标在批量操作中的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

游标(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 varchar2) is
    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 varchar2) is
    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 integer) is
  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;


3、cursor 和 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通常用在:向客户端返回结果集。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值