游标、复合类型的使用

[复制链接]
查看11 | 回复6 | 2006-9-7 10:14:43 | 显示全部楼层 |阅读模式
比如问题:
将emp表中所有员工的sal修改为100;
解决方法一:
declare
cursor cur is select empno from emp;
begin
for i in cur loop
update test_plan set sal=100 where empno=i.empno;
end loop;
commit;
end;
-----------------------------
方法二:
declare
type tp is table of test_plan.empno%type;
v_tp tp;
cursor cur is select empno from test_plan;
begin
open cur;
fetch cur bulk collect into v_tp;
close cur;
forall i in v_tp.first .. v_tp.last
update test_plan set sal=100 where empno=v_tp(i);
commit;
end;
--------------------
方法三:
declare
type tp is table of test_plan.empno%type;
v_tp tp;
begin
select empno bulk collect into v_tp from emp;
forall i in v_tp.first .. v_tp.last
update test_plan set sal=100 where empno=v_tp(i);
commit;
end;
哪种比较合适?
回复

使用道具 举报

千问 | 2006-9-7 10:14:43 | 显示全部楼层
update emp
set sal=100 ;
回复

使用道具 举报

千问 | 2006-9-7 10:14:43 | 显示全部楼层
其实我想问的是
直接循环游标好 还是 通过复合类型来循环好?
而不是想修改sal=100


回复

使用道具 举报

千问 | 2006-9-7 10:14:43 | 显示全部楼层
第一种合适,隐式,快
回复

使用道具 举报

千问 | 2006-9-7 10:14:43 | 显示全部楼层
一般表大肯定在2,3中选了,经常加limit的。
2,3的比较就在于显示游标和隐示游标的比较
显示游标好控制,隐示游标手动控制就难了!
回复

使用道具 举报

千问 | 2006-9-7 10:14:43 | 显示全部楼层
3还不错
隐式游标+bulk collect + forall
回复

使用道具 举报

千问 | 2006-9-7 10:14:43 | 显示全部楼层
如果要加行数限制就用2.
第一种其实也会批量读取(10G以上,每次100行)但是循环中只能用单行操作。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行