Oracle 10g使用游标更新或删除数据

在定义又表示必须要带有for update子句,用于在游标结果集数据上加行共享锁,以防止其他用户在相应行上执行dml操作;当select语句引用到多张表时,使用of子句可以确定哪些表要加锁,如果没有of子句,则会在select语句所引用的全部表上加锁;nowait子句用于指定不等待锁。在提取了游标数据之后,为了更新或删除当前游标行数据,必须在update或delete语句中引用where current of 子句。
--1、使用游标更新数据

1.declare
2.cursor emp_cursor is
3.--加行共享锁
4.select t.name,t.english_name from communitytype t for update;
5.--定义变量
6.v_name communitytype.name%type;
7.v_enname communitytype.english_name%type;
8.begin
9.--打开游标
10.open emp_cursor;
11.loop
12.fetch emp_cursor into v_name,v_enname;
13.exit when emp_cursor%notfound;
14.if v_name = '电子图书' then
15.update communitytype c
16.set c.english_name = 'ebook'
17.where current of emp_cursor;
18.end if;
19.end loop;
20.close emp_cursor;
21.commit;
22.end;


--2、使用游标删除数据,同上只需要将更新语句换成删除语句
--3、使用of子句在特定表上加行共享锁

1.declare
2.cursor emp_cursor is
3.--加行共享锁
4.select t.name,t.english_name from communitytype t for update of t.name;
5.--定义基于游标的记录变量
6.emp_record emp_cursor%rowtype;
7.begin
8.--打开游标
9.open emp_cursor;
10.loop
11.fetch emp_cursor into emp_record;
12.exit when emp_cursor%notfound;
13.if emp_record.name = '电子图书' then
14.update communitytype c
15.set c.english_name = 'ebook'
16.where current of emp_cursor;
17.end if;
18.end loop;
19.close emp_cursor;
20.commit;
21.end;


--4、默认情况下当前会话要一直等待对方释放锁,使用nowait子句可以避免等待锁

1.declare
2.cursor emp_cursor is
3.--加行共享锁
4.select t.name,t.english_name from communitytype t for update nowait;
5.--定义基于游标的记录变量
6.emp_record emp_cursor%rowtype;
7.begin
8.--打开游标
9.open emp_cursor;
10.loop
11.fetch emp_cursor into emp_record;
12.exit when emp_cursor%notfound;
13.if emp_record.name = '电子图书' then
14.update communitytype c
15.set c.english_name = 'ebook'
16.where current of emp_cursor;
17.end if;
18.end loop;
19.close emp_cursor;
20.commit;
21.end;


【转载地址】http://zheng12tian.iteye.com/blog/815770
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值