重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3084|回复: 1
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
楼主
发表于 2014-3-20 12:29:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在定义又表示必须要带有for update子句,用于在游标结果集数据上加行共享锁,以防止其他用户在相应行上执行dml操作;当select语句引用到多张表时,使用of子句可以确定哪些表要加锁,如果没有of子句,则会在select语句所引用的全部表上加锁;nowait子句用于指定不等待锁。在提取了游标数据之后,为了更新或删除当前游标行数据,必须在update或delete语句中引用where current of 子句。 
--1、使用游标更新数据 
  declare
  --加行共享锁 
  cursor emp_cursor is
    SELECT employee_id, last_name FROM employees for update of salary;
  --定义基于游标的记录变量  
  emp_record emp_cursor%rowtype;
  
begin
  --打开游标  
  open emp_cursor;
  loop
    fetch emp_cursor
      into emp_record;
    exit when emp_cursor%notfound;
    if emp_record.employee_id = 100 then
      dbms_output.put_line(emp_record.last_name);
      update employees c
         set c.last_name = 'abc' 
       where current of emp_cursor;
      dbms_output.put_line(emp_record.last_name);
    end if;
  end loop;
  close emp_cursor;
  
  commit;
 dbms_output.put_line(emp_record.last_name);
end;
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

沙发
 楼主| 发表于 2014-3-20 12:31:29 | 只看该作者

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

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

declare
  --加行共享锁 
  cursor emp_cursor is
    SELECT employee_id, last_name FROM employees for update of salary nowait;
  --定义基于游标的记录变量  
  emp_record emp_cursor%rowtype;
  
begin
  --打开游标  
  open emp_cursor;
  loop
    fetch emp_cursor
      into emp_record;
    exit when emp_cursor%notfound;
    if emp_record.employee_id = 100 then
      dbms_output.put_line(emp_record.last_name);
      update employees c
         set c.last_name = 'abc' 
       where current of emp_cursor;
      dbms_output.put_line(emp_record.last_name);
    end if;
  end loop;
  close emp_cursor;
  
  commit;
 dbms_output.put_line(emp_record.last_name);
end;

回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-5-15 01:16 , Processed in 0.092273 second(s), 20 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表