oracle 游标小例
有如下emp表结构:
create table ( id int, user_name varchar2(20), salary int, emp_deptno int );
插入数据如下:
insert into emp (ID, USER_NAME, SALARY, EMP_DEPTNO) values (1, 'Zhangsan', 1700, 10);insert into emp (ID, USER_NAME, SALARY, EMP_DEPTNO) values (2, 'Lisi', 4300, 20);insert into emp (ID, USER_NAME, SALARY, EMP_DEPTNO) values (3, 'Wangwu', 6600, 30);insert into emp (ID, USER_NAME, SALARY, EMP_DEPTNO) values (4, 'Qianliu', 5100, 20);insert into emp (ID, USER_NAME, SALARY, EMP_DEPTNO) values (5, 'Chenqi', 7500, 30);commit;
现在需要对不同的deptno进行工资salary的增长,deptno = 10, salary增加100, deptno = 20, salary增加200, deptno = 3, salary 增加300, 其它情况下salary增加400.
错误的存储过程如下:
declarev_increment int;v_deptno int;cursor emp_cursor isselect * from emp; beginfor i_emp in emp_cursor loopv_deptno := i_emp.emp_deptno;case v_deptnowhen 10 thenv_increment := 100;when 20 thenv_increment := 200;when 30 thenv_increment := 300;elsev_increment := 400;end case;update empset salary = salary + v_incrementwhere emp_deptno = v_deptno;end loop; end; /select * from emp;
这里存在了对emp_deptno相同的数据进行重复插入,故需要进行数据的去重,修改为:
declarev_increment int;v_deptno int;cursor emp_cursor isselect distinct emp_deptno from emp; beginfor i_emp in emp_cursor loopv_deptno := i_emp.emp_deptno;case v_deptnowhen 10 thenv_increment := 100;when 20 thenv_increment := 200;when 30 thenv_increment := 300;elsev_increment := 400;end case;update empset salary = salary + v_incrementwhere emp_deptno = v_deptno;end loop; end; /select * from emp;
这样的错误很容易犯,却不容易找到根源。
有一种更好的方法是设置更新游标,如下:
declarev_increment int;v_deptno int;cursor emp_cursor isselect * from emp for update of salary; beginfor i_emp in emp_cursor loopv_deptno := i_emp.emp_deptno;case v_deptnowhen 10 thenv_increment := 100;when 20 thenv_increment := 200;when 30 thenv_increment := 300;elsev_increment := 400;end case;update empset salary = salary + v_incrementwhere current of emp_cursor;end loop; end; /select * from emp;
转载于:https://www.cnblogs.com/bejour/p/3364283.html
oracle 游标小例相关推荐
- Oracle数据库管理每周一例-第十五期 一些工具和小技巧
Oracle数据库管理每周一例(12.2,18c,19c) 2020-09-20 第十五期 一些工具和小技巧 1.EMCC 2.MOS 3.其他一些工具和技巧 下期预告: 第十五期 一些工具和小技巧 ...
- oracle简版如何打开,如何打开和关闭Oracle游标
如何打开和关闭Oracle游标 以Oracle显示游标为主,本文讲述了如何打开Oracle游标,以及相关的实际操作,下面和小编一起来看看吧! 打开Oracle游标 使用游标中的值之前应该首先dewen ...
- oracle游标的说法,oracle游标练习题.doc
oracle游标练习题 oracle游标练习题 当查询返回结果超过一行时,就需要一个显式游标,此时用户不能使用select into语句.PL/SQL管理隐式游标,当查询开始时隐式游标打开,查询结束时 ...
- oracle游标fetch_SQL游标@@ FETCH_STATUS函数概述
oracle游标fetch SQL cursor is one of the most popular database objects. It is used to retrieve data fr ...
- Oracle数据库管理每周一例-第十七期 ADG
Oracle数据库管理每周一例(12.2,18c,19c) 2020-10-16 第十七期 ADG 1.概念 2.环境说明 3.搭建流程-主库处理 1.配置静态监听 2.配置本地命名 3.数据库参数配 ...
- 使用oracle 游标修改数据,修改oracle数据库游标
SQL游标原理和使用方法 版权声明:本文为博主原创文章,未经博主允许不得转载.https://blog.csdn.net/chinahuyong/article/details/3552248 SQL ...
- oracle scur,详解Oracle游标的简易用法
下面看下Oracle游标的简易用法,具体代码如下所示: create or replace procedure NW_DelYW(iOPERATION_ID number, sUserID varch ...
- oracle 游标当方法参数,Oracle游标、参数的使用例子
/// /// 总部审核 /// /// /// public int Update(Hashtable ht) { //修改 StringBuilder sb = new StringBuilder ...
- 重置游标oracle,Oracle游标的删除与更新实际操作步骤
以下的文章主要是介绍Oracle游标的删除与更新,我们都知道在PL/SQL中是可以使用UPDATE与DELETE语句来更新或者删除相关的数据行.显式Oracle游标只有在需要获得多行数据的情况下使用. ...
最新文章
- 程序员跳槽获25K月薪,只因他给面试官看了这6000行代码
- python编程入门书籍推荐-Python编程启蒙书籍推荐
- Edit Distance Python源码及支持包的实现
- Linux之CentOS安装composer与git
- Python基础(一)简介与安装
- 近似与精确——《狂人C》习题解答15(第三章习题5)
- How To Install IonCube Loader
- mysql php错误处理函数_PHP 错误处理
- 深度学习知识抽取:属性词、品牌词、物品词
- Oracle系统表查询方法
- 数据结构课程设计-通讯录管理系统(C语言版)
- 视频处理中各个分辨率/数字电视系统显示格式 的介绍(QCIF,CIF,4CIF,D1,720P,1080I,1080P等)
- 2019年2月已到,Java 8要收费了吗?
- 江苏高考时间2021成绩查询,小高考时间2021具体时间江苏-江苏小高考成绩查询公布时间及网站...
- 视频教程 | 与程序员进行高效沟通,三分钟带你掌握Zeplin
- linux通过文件修改密码,如何通过Linux系统来修改密码
- 【数据异常校验】拉依达准则( PauTa Criterion 或 3σ准则) 处理异常数据
- JQuery自定义属性的设置和获取
- 说一说要求 小程序自定义导航栏 的需求有多沙雕
- [ROS2]colcon build 编译选项
热门文章
- ThreadPoolExecutor(线程池)的参数
- k8s 下线node正确处理姿势
- 徒手解密 Spring Boot 中的 Starter自动化配置黑魔法
- python把文件读成字节流_Python中对字节流/二进制流的操作:struct
- php linux权限,Linux权限位
- LNMP架构——OpenResty实现缓存前移(到达Nginx前端层面)
- Python之字符处理方法大全
- 计算机游戏第72关,史上最难的游戏第72关 第72关通关攻略
- 金税接口调用实例 java_Java 常见面试题
- python图像配准的原理_python利用sift和surf进行图像配准