Oracle使用游标更新数据

前端之家收集整理的这篇文章主要介绍了Oracle使用游标更新数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用游标修改数据

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

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

注意:

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

优化:

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

猜你在找的Oracle相关文章