Oracle辅导:浅谈关于ORACLE中的游标Cursor总结(2)
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;
显式和隐式游标的区别:
尽量使用隐式游标,避免编写附加的游标控制代码(声明,打开,获取,关闭),也不需要声明变量来保存从游标中获取的数据。
Oracle辅导:浅谈关于ORACLE中的游标Cursor总结(2)
</p> 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 tab2as select empno ID, ename NAME, sal SALARY from emp where 1=2;
create or replace procedure REF_BULK is
/*定义复杂类型 */
type empcurtypis ref cursor;
type idlistis table of emp.empno%type;
type namelistis table of emp.ename%type;
type sallistis table of emp.sal%type;
/* 定义变量*/
emp_cvempcurtyp;
idsidlist;
names namelist;
sals sallist;
row_cnt number;
begin
open emp_cv for select empno, ename, sal from emp;
fetch emp_cvBULK COLLECTINTO 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;
FORALLiINids.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;
Oracle辅导:浅谈关于ORACLE中的游标Cursor总结(2)
</p> 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通常用在:向客户端返回结果集。
页:
[1]