Oracle使用游标更新数据

2016年11月20日 13:15:49 hzwy23 阅读数:5313


友情推广

###使用游标修改数据

####定义一个游标,游标名称为 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;
beginopen mycursor(7369);while(true) loopfetch 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;
beginopen mycursor(7369);while(true) loopfetch mycursor into he; exit when mycursor%notfound;delete from emp where current of mycursor;end loop;
end;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

###注意:

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

####优化:

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

https://blog.csdn.net/hzwy23/article/details/53240333

Oracle游标之select for update和where current of 语句

2013年06月27日 10:57:09 luckystar2008 阅读数:3150

转载http://hanjiangduqiao.blog.163.com/blog/static/613105442011431111153601

使用select for update 语句可以使用行锁锁定你要更改的记录.当遇到下一个commit和rollback语句时会被释放.

The Select For Update statement allows you to lock the records in the cursor result set. You are not required to make changes to the records in order to use this statement. The record locks are released when the next commit or rollback statement is issued.

语法如下:The syntax for the Select For Update is:

CURSOR cursor_name
IS
   select_statement
   FOR UPDATE [of column_list] [NOWAIT];

当你要使用游标进行更新和删除操作时,则需要使用where current of 语句,这个是标示出当前游标的位置.

If you plan on updating or deleting records that have been referenced by a select for update statement, you can use the Where Current Of statement.

语法如下:The syntax for the Where Current Of statement is either:

UPDATE table_name
    SET set_clause
    WHERE CURRENT OF cursor_name;

OR

DELETE FROM table_name
WHERE CURRENT OF cursor_name;

在这时的例子中我采用上面所建的test表来试验.

2.1 更新.

SQL> DECLARE
  2  CURSOR  test_cur IS SELECT  * FROM test
  3  FOR UPDATE OF sal;
  4  BEGIN
  5  FOR test_rec IN  test_cur LOOP
  6  UPDATE test
  7  SET sal = test_rec.sal +1
  8  WHERE CURRENT OF test_cur;
  9  END LOOP;
 10  COMMIT;
 11  END;
 12  /

PL/SQL 过程已成功完成。

2.2 删除.

SQL> DECLARE
  2  CURSOR test_cur IS select * from test for update;
  3  BEGIN
  4  FOR test_rec IN test_cur LOOP
  5  DELETE FROM test WHERE CURRENT OF test_cur;
  6  END LOOP;
  7  END;
  8  /

PL/SQL 过程已成功完成。

文中的游标只是简单的使用,在记录到pl/sql详细编程的时候会再讲到时会结合oracle的执行计划并一起讨论它们的执行效率.

注:文中的英文注解部分出自:http://www.techonthenet.com/oracle/cursors/current_of.php

https://blog.csdn.net/qincidong/article/details/9185693

Oracle使用游标更新数据 Oracle游标之select for update和where current of 语句相关推荐

  1. oracle游标添加数据,Oracle使用游标更新数据

    1. 使用游标修改数据 定义一个游标,游标名称为 mycursor 更新scott用户中emp表中empno为7369的销售额 -- Created on 2015/11/30 by ZHANW de ...

  2. oracle批量将id更新为uuid,oracle批量新增更新数据

    本博客介绍一下Oracle批量新增数据和更新数据的sql写法,业务场景是这样的,往一张关联表里批量新增更新数据,然后,下面介绍一下批量新增和更新的写法: 批量新增数据 对于批量新增数据,介绍两种方法 ...

  3. mysql中游标能不能更新数据库_数据库游标更新数据

    SQLServer游标(Cursor)简介和使用说明 游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力.我们可以把 ...

  4. oracle查询并更新数据库,oracle数据库查询和更新

    package sqltest; import java.sql.*; import parameter.BaseCanShu; public class PublicDbOracle { stati ...

  5. oracle有条件插入数据,Oracle有条件地插入数据

    方法一: declare iExists int; begin select count(*) into iExists from表 where 条件; if iExists=0 then inser ...

  6. oracle寻找第一条数据,oracle 获取第一条数据

    关于取第一条数据的sql特此作了一个例子如下: SELECT * FROM tableName where fd_rt = 'A' --and rownum=1 ORDER BY fd_date DE ...

  7. oracle存储过程批量导入数据,Oracle 存储过程之批量添加数据

    –vc_trade_id 复制交易方案id –vc_new_trade_id 新id –vc_scheme_date 系统逻辑时间 –l_trade_stage 交易方案轮次 –vc_trading_ ...

  8. 使用oracle 游标修改数据,Oracle 函数施行修改和游标传递

    Oracle 函数执行修改和游标传递 ORA-14551:不能在查询语句中执行dml语句,开始误以为函数里不能执行DML或DDL语句,后查到前辈通过采用自治事务解决,在此基础上演例 传递SQL语句对数 ...

  9. Oracle 实验4 更新数据

    一.实验目的 掌握数据的更新 二.实验内容 1.将一个新生插入到Student表中,其学号为:0821105,姓名为:陈冬, 性别为:男,年龄18岁,信息管理系学生. 2.在SC表中插入一条新记录,学 ...

最新文章

  1. 22.加密与安全相关,证书申请CA(gpg,openssl)
  2. mysql启动多端口
  3. 比亚迪汉家族3月热销12359辆 汉EV单车销量破万
  4. iQOO 8首次采用三星E5屏幕:2021年最好的手机屏幕
  5. 集成学习 Bagging, Boosting, Stacking
  6. Kerberos的工作原理
  7. Tbase 源码 (三)
  8. esp8266网页控制RGB灯颜色
  9. 电脑系统安装后桌面图标隔开很宽怎么调?
  10. Vmware安装win10报错:operating system not found
  11. 物联网智能开关平台源码
  12. android 无法后台运行,安卓模拟器无法后台?
  13. 有关信息学竞赛的常见问题
  14. PathInfo模式的支持
  15. 游戏随机地图生成方法
  16. 如何使用高防CDN防御DDOS攻击呢?
  17. 专车新规或下周发布,估计有大量司机流失
  18. Juniper SRX操作系统软件升级
  19. mysql按条件查询left_mysql-查询条件下的LEFT JOIN
  20. ChatGPT vscode中文插件

热门文章

  1. 如何安装R以及RStudio?打开RStudio页面告诉你没安装R或者出现页面空白问题
  2. Vertica数据库介绍
  3. 密苏里大学理工学院计算机,密苏里大学理工学院
  4. 去哪家期货公司如何开户?
  5. 名帖04 李阳冰 篆书《城隍庙碑》
  6. 【php学习之路】微信公众帐号
  7. 直方图均衡化取整怎么计算_玩转直方图处理之直方图均衡化、规定化
  8. docker学习(八)深入浅出理解 dockerFille
  9. 生物信息学linux安装,构建生物信息学环境-1(Win10 Linux子系统的安装)
  10. Win10资源管理器CPU持续占用20%解决方法