加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle使用游标更新数据

发布时间:2020-12-12 15:42:23 所属栏目:百科 来源:网络整理
导读:使用游标修改数据 定义一个游标,游标名称为 mycursor 更新scott用户中emp表中empno为7369的销售额 -- Created on 2015/11/30 by ZHANW declare he emp%rowtype; cursor mycursor(pid integer) is select * from emp where empno = pid for update ; begin o

使用游标修改数据

定义一个游标,游标名称为 mycursor

更新scott用户中emp表中empno为7369的销售额
-- Created on 2015/11/30 by ZHANW 
declare 
  he emp%rowtype;
  cursor mycursor(pid integer) is select * from emp where empno = pid for update;
begin open mycursor(7369);
  while(true) loop
     fetch mycursor into he; 
     exit when mycursor%notfound;
     update emp set sal = 1111 where current of mycursor;
  end loop;
end;
-- Created on 2015/11/30 by ZHANW 
declare 
  he emp%rowtype;
  cursor mycursor(pid integer) is select * from emp where empno = pid for update;
begin open mycursor(7369);
  while(true) loop
     fetch mycursor into he; 
     exit when mycursor%notfound;
     delete from emp where current of mycursor;
  end loop;
end;

注意:

delete语句一定要写在exit后面,不然可能会报错。

优化:

在定义游标时,可以在for update 后面添加 of 字段或者nowait。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读