Oracle游标总结三
- -- 声明游标;CURSOR cursor_name IS select_statement
- --For 循环游标
--(1)定义游标
--(2)定义游标变量
--(3)使用for循环来使用这个游标,for循环时
- declare
- cursor c_job
- is
- select empno,ename,job,sal from emp
- where job='MANAGER';
- c_row c_job%rowtype;
- begin
- open c_job;
- for c_row in c_job loop
- dbms_output.put_line(c_row.empno||c_row.ename)
- end loop;
- close c_job;
- end
- --Fetch游标
--使用的时候必须要明确的打开和关闭
- declare
- cursor c_job
- is
- select empno,ename from emp
- where job='MANAGER';
- c_row c_job%rowtype;
- begin
- open c_job;
- loop
- fetch c_job into c_row;
- --判读是否提取到值,没取到值就退出
- --取到值c_job%notfound 是false
- --取不到值c_job%notfound 是true
- exit when c_job%notfound;
- end loop
- close c_job;
- end;
- --1:任意执行一个update操作,用隐式游标sql的属性%found,%notfound,%rowcount,%isopen观察update语句的执行情况。
- begin
- update emp set ENAME='ALEARK' WHERE EMPNO=7469;
- if sql%isopen then
- dbms_output.put_line('Openging');
- else
- dbms_output.put_line('closing');
- end if;
- if sql%found then
- dbms_output.put_line('游标指向了有效行');--判断游标是否指向有效行
- else
- dbms_output.put_line('Sorry');
- end if;
- if sql%notfound then
- dbms_output.put_line('Also Sorry');
- else
- dbms_output.put_line('Haha');
- end if;
- dbms_output.put_line(sql%rowcount);
- exception
- when no_data_found then
- dbms_output.put_line('Sorry No data');
- when too_many_rows then
- dbms_output.put_line('Too Many rows');
- end;
- declare
- empNumber emp.EMPNO%TYPE;
- empName emp.ENAME%TYPE;
- begin
- if sql%isopen then
- dbms_output.put_line('Cursor is opinging');
- else
- dbms_output.put_line('Cursor is Close');
- end if;
- if sql%notfound then
- dbms_output.put_line('No Value');
- else
- dbms_output.put_line(empNumber);
- end if;
- dbms_output.put_line(sql%rowcount);
- dbms_output.put_line('-------------');
- select EMPNO,ENAME into empNumber,empName from emp where EMPNO=7499;
- dbms_output.put_line(sql%rowcount);
- if sql%isopen then
- dbms_output.put_line('Cursor is opinging');
- else
- dbms_output.put_line('Cursor is Closing');
- end if;
- if sql%notfound then
- dbms_output.put_line('No Value');
- else
- dbms_output.put_line(empNumber);
- end if;
- exception
- when no_data_found then
- dbms_output.put_line('No Value');
- when too_many_rows then
- dbms_output.put_line('too many rows');
- end;
- --2,使用游标和loop循环来显示所有部门的名称
--游标声明
- declare
- cursor csr_dept
- is
- select dname from depth;
- row_dept csr_dept%rowtype;
- begin
- for row_dept in csr_dept loop
- dbms_output.put_line(row_dept.dname);
- end loop;
- end;
- --3,使用游标和while循环来显示所有部门的的地理位置(用%found属性)
- declare
- cursor csr_TestWhile
- is
- select LOC from Depth;
- row_loc csr_TestWhile%rowtype;
- begin
- open csr_TestWhile;
- --必须先fetch一遍,才能进行%found判断,否则一直都是空为false
- fetch csr_TestWhle into row_loc;
- while csr_TestWhile%found loop
- dbms_output.put_line(row_loc.LOC);
- fetch csr_TestWhile into row_loc;
- end loop;
- close csr_TestWhile;
- end
- --4,接收用户输入的部门编号,用for循环和游标,打印出此部门的所有雇员的所有信息(使用循环游标)
--CURSOR cursor_name[(parameter[,parameter],...)] IS select_statement;
--定义参数的语法如下:Parameter_name [IN] data_type[{:=|DEFAULT} value]
- declare
- --定义带参数的游标
- cursor c_dept(p_deptNo number)
- is
- select * from emp where emp.depno=p_deptNo;
- r_emp emp%rowtype;
- begin
- --遍历游标,调用游标传参数
- for r_emp in c_dept(20) loop
- dbms_output.put_line('员工号:'||r_emp.EMPNO||'员工名:'||r_emp.ENAME||'工资:'||r_emp.SAL);
- end loop;
- end;
- --6:用更新游标来为雇员加佣金:(用if实现,创建一个与emp表一摸一样的emp1表,对emp1表进行修改操作),并将更新前后的数据输出出来
- create table emp1 as select * from emp;
- declare
- cursor
- csr_Update
- is
- select * from emp1 for update OF SAL;
- empInfo csr_Update%rowtype;
- saleInfo emp1.SAL%TYPE;
- begin
- FOR empInfo IN csr_Update LOOP
- IF empInfo.SAL<1500 THEN
- saleInfo:=empInfo.SAL*1.2;
- elsif empInfo.SAL<2000 THEN
- saleInfo:=empInfo.SAL*1.5;
- elsif empInfo.SAL<3000 THEN
- saleInfo:=empInfo.SAL*2;
- END IF;
- UPDATE emp1 SET SAL=saleInfo WHERE CURRENT OF csr_Update;
- END LOOP;
- END;
- --7:编写一个PL/SQL程序块,对名字以‘A’或‘S’开始的所有雇员按他们的基本薪水(sal)的10%给他们加薪(对emp1表进行修改操作)
- declare
- cursor csr_addSal
- is select sal from emp where ename like 'A%' or ename like'S%';
- r_addSal csr_addSal%rowtype;
- saleInfo emp.sal%type;
- begin
- for r_addSal in csr_addSal loop
- saleInfo := r_addSal.sal*1.1;
- update emp set sal = saleInfo where ename like 'A%' or ename like'S%';
- end loop;
- end
- --8:编写一个PL/SQL程序块,对所有雇员按他们的基本薪水(sal)的20%为他们加薪,
--如果增加的薪水大于300就取消加薪(对emp1表进行修改操作,并将更新前后的数据输出出来)
- declare
- cursor
- crs_UpadateSal
- is select sal from emp;
- r_UpadateSal crs_UpadateSal%rowtype
- salAdd emp.sal%type;
- salInfo emp.sal%type;
- begin
- for r_UpadateSal in crs_UpadateSal loop
- salAdd:=r_UpadateSal.sal*0.2;
- if salAdd>300 then
- salInfo:=r_r_UpadateSal.sal;
- else
- salInfo:=r_UpadateSal.sal+salAdd;
- end if
- update emp set sal=salInfo where current of crs_UpadateSal;
- end loop;
- end;
- --9:将每位员工工作了多少年零多少月零多少天输出出来
--近似
--CEIL(n)函数:取大于等于数值n的最小整数
--FLOOR(n)函数:取小于等于数值n的最大整数
--truc的用法 http://publish.it168.com/2005/1028/20051028034101.shtml
- declare
- cursor
- crs_workDay
- is select ename,hirdate,trunc(months_between(sysdate,hirdate)/12) as years,
- trunc(mod(months_between(sysdate,hirdate),12)) as monthss,
- trunc(mod(sysdate-hirdate),365),12) as days
- from emp
- r_workDay crs_workDay%rowtype;
- begin
- for r_workDay in crs_workDay loop
- dbms_output.put_line(r_WorkDay.ENAME||'已经工作了'||r_WorkDay.SPANDYEARS||'年,零'||r_WorkDay.months||'月,零'||r_WorkDay.days||'天');
- end loop;
- end
- --10:输入部门编号,按照下列加薪比例执行(用CASE实现,创建一个emp1表,修改emp1表的数据),并将更新前后的数据输出出来
-- deptno raise(%)
-- 10 5%
-- 20 10%
-- 30 15%
-- 40 20%
-- 加薪比例以现有的sal为标准
--CASE expr WHEN comparison_expr THEN return_expr
--[, WHEN comparison_expr THEN return_expr]... [ELSE else_expr] END
- declare
- cursor
- crs_caseTest
- is select deptNo,sal from emp;
- r_caseTest crs_caseTest%rowtype;
- salInfo emp.sal%rowtype;
- begin
- for r_caseTest in crs_caseTest loop
- case
- when r_caseTest.deptNo=10 then
- salInfo = r_caseTest.sal*1.05;
- when r_caseTest.deptNo=10 then
- salInfo = r_caseTest.sal*1.1;
- when r_caseTest.deptNo=10 then
- salInfo = r_caseTest.sal*1.15;
- when r_caseTest.deptNo=10 then
- salInfo = r_caseTest.sal*1.2;
- end case;
- update emp set sal = salInfo where current of crs_caseTest;
- end loop;
- end;
- --13:对每位员工的薪水进行判断,如果该员工薪水高于其所在部门的平均薪水,则将其薪水减50元,输出更新前后的薪水,员工姓名,所在部门编号。
--AVG([distinct|all] expr) over (analytic_clause)
---作用:
--按照analytic_clause中的规则求分组平均值。
--分析函数语法:
--FUNCTION_NAME(<argument>,<argument>...)
--OVER
--(<Partition-Clause><Order-by-Clause><Windowing Clause>)
--PARTITION子句
--按照表达式分区(就是分组),如果省略了分区子句,则全部的结果集被看作是一个单一的组
- DECLARE
- cursor csr_agvSal
- is select empno,sal,avg(sal) over (parition by deptno) as avgSal from emp;
- r_avgSal csr_agvSal%rowtype;
- salInfo emp.sal%type;
- begin
- for r_avgSal in csr_agvSal loop
- if r_avgSal.sal>r_avgSal.avgSal then
- salInfo := r_avgSal.sal-50;
- end if;
- update emp set sal = salInfo where current of csr_agvSal;
- end loop;
- end;
Oracle游标总结三的更多相关文章
- Oracle游标的使用示例
此文是使用Oracle游标的几种方式,for...in会自动打开游标,fetch...into需要手动打开游标,游标类似于一个只会往前移动的指针,每次指向数据集中的一行数据,通过游标可以打开数据集,也 ...
- Oracle游标介绍
Oracle游标使用详解: 游标: 用来查询数据库,获取记录集合(结果集)的指针,我们所说的游标通常是指显式游标,因此从现在起没有特别指明的情况,我们所说的游标都是指显式游标.要在程序中使用游标,必须 ...
- Oracle游标解析
本节对Oracle中的游标进行详细讲解. 本节所举实例来源Oracle中scott用户下的emp表dept表: 一.游标: 1.概念: 游标的本质是一个结果集resultset,主要用来临时存储从数据 ...
- Oracle学习笔记三 SQL命令
SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)
- Oracle 游标示例,带异常处理
Oracle游标示例一则,带异常处理. DECLARE CURSOR c_dl IS SELECT ID, NSRSBH, WSPZXH, ZXYY_DM, HZRQ, SWJG_DM, GXSJ F ...
- Oracle游标带参数
Oracle游标是可以带参数的,而SqlServer的游标就不可以了 create or replace procedure a as cursor b(c_id int)is select * fr ...
- Oracle 游标使用(转)
这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 ; ; dbms_output.put_line(sql) loop dbms_output.put_line( ; ; ; r_te ...
- Oracle 游标使用全解(转)
转自:http://www.cnblogs.com/sc-xx/archive/2011/12/03/2275084.html 这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 -- ...
- Oracle游标动态赋值
1. oracle游标动态赋值的小例子 -- 实现1:动态给游标赋值 -- 实现2:游标用表的rowtype声明,但数据却只配置表一行的某些字段时,遍历游标时需fetch into到精确字段 CREA ...
随机推荐
- IOS测试程序运行耗时
iOS设备相对于电脑,内存和处理能力有限,所以一段代码或者程序运行的时间需要时刻注意,这里提供两种获取精确时间的方法. 方法一:使用系统时间 NSDate* tmpStartData = [[NSDa ...
- Object C语法学习
#synthesize关键字: 根据@property设置,自动生成成员变量相应的存取方法,从而可以使用点操作符来方便的存取该成员变量 . @implementation 关键字,表明类的实现 @en ...
- Face The Right Way---hdu3276(开关问题)
题目链接:http://poj.org/problem?id=3276 题意:n牛头排成一排,每头牛两个状态,向前或向后,为了让所有的牛都向前,现在有一个机器 每次 能控制连续K头牛转换自己的状态,求 ...
- CodeTimer
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- Web设计者和开发者必备的28个Chrome插件
摘要 对于许多Web设计者和开发者来说,Firefox浏览器是无法超越的,对于其他人Chrome正在蚕食Firefox的浏览器市场. 在过去的两年,谷歌Chrome浏览器的发布以来,引起了人们激烈争论 ...
- 简述Docker镜像、容器、仓库概念
Docker镜像 Docker镜像(Image)类似于虚拟机的镜像,可以将他理解为一个面向Docker引擎的只读模板,包含了文件系统. 例如:一个镜像可以完全包含了Ubuntu操作系统环境,可以把它称 ...
- ionic环境搭建和安装
1. 安装node环境 nodeJs环境的安装很简单,去官网下载最新版的NodeJs直接安装即可. Node官网: https://nodejs.org/ 安装完成后配置环境变量,计算机->属性 ...
- Json.Net Demo2
新建一个空的Web项目,名称JsonServer,该网页实现Ajax数据请求和响应. 添加Newtonsoft.Json.dll的Dll引用,添加JQuery API文件,目录结构如下: 新建一个Pe ...
- MongoDB概念解析
数据库 MongoDB默认把_id设置为主键(_开头的键是保留的) 数据库名必须为小写 RDBMS与MongoDB对应的术语比较 需注意 文档中的键/值对是有序的. 文档中的值不仅可以是在双引号里面的 ...
- glusterFS安装维护文档
.规划: .依赖包 yum install libibverbs librdmacm xfsprogs nfs-utils rpcbind libaio liblvm2app lvm2-devel l ...