oracle游标的更新与删除数据
2015-08-25 14:48
537 查看
通过使用显示游标,不仅可以一行一行地处理SELECT语句的结果,而且也可以更新或删除当前游标行的数据。注意,如果要通过游标更新或删除数据,在定义游标时必须要带有FOR UPDATE 子句,语法如下:
在提取了游标数据之后,为了更新或删除当前游标行数据,必须在update或delete语句中引用where current of子句,语法如下:
cursor cursor_name is select ...for update;
在提取了游标数据之后,为了更新或删除当前游标行数据,必须在update或delete语句中引用where current of子句,语法如下:
update table_name set column=...where current of cursor_name;
delete table_name where current of cursor_name;
1.使用游标更新数据
下面以给工资低于2000的雇员增加100元工资为例,说明使用显示游标更新数据的方法。示例如下:declare cursor emp_cursor is select ename,sal from emp for update; v_ename emp.ename%type;--定义变量 v_sal emp.sal%type; begin open emp_cursor;--打开游标 loop fetch emp_cursor into v_ename,v_sal;--提取游标数据赋值给变量 exit when emp_cursor%notfound; if v_sal <2000 then --当变量v_sal 小于2000时执行更新操作 update emp set sal=sal+100 where currentof emp_cursor; end if ; end loop; close emp_cursor;--关闭游标 end;
2.使用游标删除数据
下面以解雇部门号为30的所有雇员为例,说明使用显示游标删除数据的方法,示例如下:declare cursor emp_cursor is select ename,sal ,deptno from emp for update; v_ename emp.ename%type;--定义变量 v_sal emp.sal%type; v_deptno emp.deptno%type; begin open emp_cursor;--打开游标 loop fetch emp_cursor into v_ename,v_sal,v_deptno;--提取游标数据赋值给变量 exit when emp_cursor%notfound; if v_deptno =30 then --当变量v_deptno等于30时执行删除操作 delete from emp where current of emp_cursor; end if ; end loop; close emp_cursor;--关闭游标 end;
相关文章推荐
- 优化Oracle数据库查询10个方法
- ORACLE中的varchar2()与nvarchar2()的讲解
- Oracle的悲观锁和乐观锁---摘抄
- mySql与oracle分页技术原理与实现
- ORACLE初始化参数文件概述
- Oracle的启动过程
- Oracle存储过程异常信息的显示
- 如何简化 Oracle Linux 上 Oracle Database 的安装
- Oracle使用并行索引需要注意的问题
- jdbc连接oracle报错No suitable driver found for jdbc
- oracle start with
- DBA必知的170张Oracle常用动态性能表介绍
- Oracle数据库包含四种文件类型
- [转载] ORACLE 多表关联 UPDATE 语句
- Oracle高效sql语句整理
- Oracle数据库高效sql语句的整理
- ORACLE-017:SQL优化-is not null和nvl
- ORACLE-017:SQL优化-is not null和nvl
- oracle with as 用法
- Oracle Instant Client(即时客户端) 安装与配置