1. -- 声明游标;CURSOR cursor_name IS select_statement
  2.  
  3. --For 循环游标
    --(1)定义游标
    --(2)定义游标变量
    --(3)使用for循环来使用这个游标,for循环时
  1. declare
  2. cursor c_job
  3. is
  4. select empno,ename,job,sal from emp
  5. where job='MANAGER';
  6. c_row c_job%rowtype;
  7. begin
  8. open c_job;
  9. for c_row in c_job loop
  10. dbms_output.put_line(c_row.empno||c_row.ename)
  11. end loop;
  12. close c_job;
  13. end
  1. --Fetch游标
    --使用的时候必须要明确的打开和关闭
  1. declare
  2. cursor c_job
  3. is
  4. select empno,ename from emp
  5. where job='MANAGER';
  6. c_row c_job%rowtype;
  7. begin
  8. open c_job;
  9. loop
  10. fetch c_job into c_row;
  11. --判读是否提取到值,没取到值就退出
  12. --取到值c_job%notfound false
  13. --取不到值c_job%notfound true
  14. exit when c_job%notfound;
  15. end loop
  16. close c_job;
  17. end;
  1. --1:任意执行一个update操作,用隐式游标sql的属性%found,%notfound,%rowcount,%isopen观察update语句的执行情况。
  1. begin
  2. update emp set ENAME='ALEARK' WHERE EMPNO=7469;
  3. if sql%isopen then
  4. dbms_output.put_line('Openging');
  5. else
  6. dbms_output.put_line('closing');
  7. end if;
  8. if sql%found then
  9. dbms_output.put_line('游标指向了有效行');--判断游标是否指向有效行
  10. else
  11. dbms_output.put_line('Sorry');
  12. end if;
  13. if sql%notfound then
  14. dbms_output.put_line('Also Sorry');
  15. else
  16. dbms_output.put_line('Haha');
  17. end if;
  18. dbms_output.put_line(sql%rowcount);
  19. exception
  20. when no_data_found then
  21. dbms_output.put_line('Sorry No data');
  22. when too_many_rows then
  23. dbms_output.put_line('Too Many rows');
  24. end;
  1. declare
  2. empNumber emp.EMPNO%TYPE;
  3. empName emp.ENAME%TYPE;
  4. begin
  5. if sql%isopen then
  6. dbms_output.put_line('Cursor is opinging');
  7. else
  8. dbms_output.put_line('Cursor is Close');
  9. end if;
  10. if sql%notfound then
  11. dbms_output.put_line('No Value');
  12. else
  13. dbms_output.put_line(empNumber);
  14. end if;
  15. dbms_output.put_line(sql%rowcount);
  16. dbms_output.put_line('-------------');
  17.  
  18. select EMPNO,ENAME into empNumber,empName from emp where EMPNO=7499;
  19. dbms_output.put_line(sql%rowcount);
  20.  
  21. if sql%isopen then
  22. dbms_output.put_line('Cursor is opinging');
  23. else
  24. dbms_output.put_line('Cursor is Closing');
  25. end if;
  26. if sql%notfound then
  27. dbms_output.put_line('No Value');
  28. else
  29. dbms_output.put_line(empNumber);
  30. end if;
  31. exception
  32. when no_data_found then
  33. dbms_output.put_line('No Value');
  34. when too_many_rows then
  35. dbms_output.put_line('too many rows');
  36. end;
  1. --2,使用游标和loop循环来显示所有部门的名称
    --游标声明
  1. declare
  2. cursor csr_dept
  3. is
  4. select dname from depth;
  5. row_dept csr_dept%rowtype;
  6. begin
  7. for row_dept in csr_dept loop
  8. dbms_output.put_line(row_dept.dname);
  9. end loop;
  10. end;
  1. --3,使用游标和while循环来显示所有部门的的地理位置(用%found属性)
  1. declare
  2. cursor csr_TestWhile
  3. is
  4. select LOC from Depth;
  5. row_loc csr_TestWhile%rowtype;
  6. begin
  7. open csr_TestWhile;
  8. --必须先fetch一遍,才能进行%found判断,否则一直都是空为false
  9. fetch csr_TestWhle into row_loc;
  10. while csr_TestWhile%found loop
  11. dbms_output.put_line(row_loc.LOC);
  12. fetch csr_TestWhile into row_loc;
  13. end loop;
  14. close csr_TestWhile;
  15. end
  1. --4,接收用户输入的部门编号,用for循环和游标,打印出此部门的所有雇员的所有信息(使用循环游标)
    --CURSOR cursor_name[(parameter[,parameter],...)] IS select_statement;
    --定义参数的语法如下:Parameter_name [IN] data_type[{:=|DEFAULT} value]
  1. declare
  2. --定义带参数的游标
  3. cursor c_dept(p_deptNo number)
  4. is
  5. select * from emp where emp.depno=p_deptNo;
  6. r_emp emp%rowtype;
  7. begin
  8. --遍历游标,调用游标传参数
  9. for r_emp in c_dept(20) loop
  10. dbms_output.put_line('员工号:'||r_emp.EMPNO||'员工名:'||r_emp.ENAME||'工资:'||r_emp.SAL);
  11. end loop;
  12. end;
  1. --6:用更新游标来为雇员加佣金:(用if实现,创建一个与emp表一摸一样的emp1表,对emp1表进行修改操作),并将更新前后的数据输出出来
  1. create table emp1 as select * from emp;
  2.  
  3. declare
  4. cursor
  5. csr_Update
  6. is
  7. select * from emp1 for update OF SAL;
  8. empInfo csr_Update%rowtype;
  9. saleInfo emp1.SAL%TYPE;
  10. begin
  11. FOR empInfo IN csr_Update LOOP
  12. IF empInfo.SAL<1500 THEN
  13. saleInfo:=empInfo.SAL*1.2;
  14. elsif empInfo.SAL<2000 THEN
  15. saleInfo:=empInfo.SAL*1.5;
  16. elsif empInfo.SAL<3000 THEN
  17. saleInfo:=empInfo.SAL*2;
  18. END IF;
  19. UPDATE emp1 SET SAL=saleInfo WHERE CURRENT OF csr_Update;
  20. END LOOP;
  21. END;
  1. --7:编写一个PL/SQL程序块,对名字以‘A’或‘S’开始的所有雇员按他们的基本薪水(sal)的10%给他们加薪(对emp1表进行修改操作)
  1. declare
  2. cursor csr_addSal
  3. is select sal from emp where ename like 'A%' or ename like'S%';
  4. r_addSal csr_addSal%rowtype;
  5. saleInfo emp.sal%type;
  6. begin
  7. for r_addSal in csr_addSal loop
  8. saleInfo := r_addSal.sal*1.1;
  9. update emp set sal = saleInfo where ename like 'A%' or ename like'S%';
  10. end loop;
  11. end
  1. --8:编写一个PL/SQL程序块,对所有雇员按他们的基本薪水(sal)的20%为他们加薪,
    --如果增加的薪水大于300就取消加薪(对emp1表进行修改操作,并将更新前后的数据输出出来)
  1. declare
  2. cursor
  3. crs_UpadateSal
  4. is select sal from emp;
  5. r_UpadateSal crs_UpadateSal%rowtype
  6. salAdd emp.sal%type;
  7. salInfo emp.sal%type;
  8. begin
  9. for r_UpadateSal in crs_UpadateSal loop
  10. salAdd:=r_UpadateSal.sal*0.2;
  11. if salAdd>300 then
  12. salInfo:=r_r_UpadateSal.sal;
  13. else
  14. salInfo:=r_UpadateSal.sal+salAdd;
  15. end if
  16. update emp set sal=salInfo where current of crs_UpadateSal;
  17. end loop;
  18. end;
  1. --9:将每位员工工作了多少年零多少月零多少天输出出来
    --近似
    --CEIL(n)函数:取大于等于数值n的最小整数
    --FLOOR(n)函数:取小于等于数值n的最大整数
    --truc的用法 http://publish.it168.com/2005/1028/20051028034101.shtml
  1. declare
  2. cursor
  3. crs_workDay
  4. is select ename,hirdate,trunc(months_between(sysdate,hirdate)/12) as years,
  5. trunc(mod(months_between(sysdate,hirdate),12)) as monthss,
  6. trunc(mod(sysdate-hirdate),365),12) as days
  7. from emp
  8. r_workDay crs_workDay%rowtype;
  9. begin
  10. for r_workDay in crs_workDay loop
  11. dbms_output.put_line(r_WorkDay.ENAME||'已经工作了'||r_WorkDay.SPANDYEARS||'年,零'||r_WorkDay.months||'月,零'||r_WorkDay.days||'天');
  12. end loop;
  13. end
  1. --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
  1. declare
  2. cursor
  3. crs_caseTest
  4. is select deptNo,sal from emp;
  5. r_caseTest crs_caseTest%rowtype;
  6. salInfo emp.sal%rowtype;
  7. begin
  8. for r_caseTest in crs_caseTest loop
  9. case
  10. when r_caseTest.deptNo=10 then
  11. salInfo = r_caseTest.sal*1.05;
  12. when r_caseTest.deptNo=10 then
  13. salInfo = r_caseTest.sal*1.1;
  14. when r_caseTest.deptNo=10 then
  15. salInfo = r_caseTest.sal*1.15;
  16. when r_caseTest.deptNo=10 then
  17. salInfo = r_caseTest.sal*1.2;
  18. end case;
  19. update emp set sal = salInfo where current of crs_caseTest;
  20. end loop;
  21. end;
  1. --13:对每位员工的薪水进行判断,如果该员工薪水高于其所在部门的平均薪水,则将其薪水减50元,输出更新前后的薪水,员工姓名,所在部门编号。
    --AVG([distinct|all] expr) over (analytic_clause)
    ---作用:
    --按照analytic_clause中的规则求分组平均值。
    --分析函数语法:
    --FUNCTION_NAME(<argument>,<argument>...)
    --OVER
    --(<Partition-Clause><Order-by-Clause><Windowing Clause>)
    --PARTITION子句
    --按照表达式分区(就是分组),如果省略了分区子句,则全部的结果集被看作是一个单一的组
  1. DECLARE
  2. cursor csr_agvSal
  3. is select empno,sal,avg(sal) over (parition by deptno) as avgSal from emp;
  4. r_avgSal csr_agvSal%rowtype;
  5. salInfo emp.sal%type;
  6. begin
  7. for r_avgSal in csr_agvSal loop
  8. if r_avgSal.sal>r_avgSal.avgSal then
  9. salInfo := r_avgSal.sal-50;
  10. end if;
  11. update emp set sal = salInfo where current of csr_agvSal;
  12. end loop;
  13. end;
  1.  

Oracle游标总结三的更多相关文章

  1. Oracle游标的使用示例

    此文是使用Oracle游标的几种方式,for...in会自动打开游标,fetch...into需要手动打开游标,游标类似于一个只会往前移动的指针,每次指向数据集中的一行数据,通过游标可以打开数据集,也 ...

  2. Oracle游标介绍

    Oracle游标使用详解: 游标: 用来查询数据库,获取记录集合(结果集)的指针,我们所说的游标通常是指显式游标,因此从现在起没有特别指明的情况,我们所说的游标都是指显式游标.要在程序中使用游标,必须 ...

  3. Oracle游标解析

    本节对Oracle中的游标进行详细讲解. 本节所举实例来源Oracle中scott用户下的emp表dept表: 一.游标: 1.概念: 游标的本质是一个结果集resultset,主要用来临时存储从数据 ...

  4. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

  5. Oracle 游标示例,带异常处理

    Oracle游标示例一则,带异常处理. DECLARE CURSOR c_dl IS SELECT ID, NSRSBH, WSPZXH, ZXYY_DM, HZRQ, SWJG_DM, GXSJ F ...

  6. Oracle游标带参数

    Oracle游标是可以带参数的,而SqlServer的游标就不可以了 create or replace procedure a as cursor b(c_id int)is select * fr ...

  7. Oracle 游标使用(转)

    这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 ; ; dbms_output.put_line(sql) loop dbms_output.put_line( ; ; ; r_te ...

  8. Oracle 游标使用全解(转)

    转自:http://www.cnblogs.com/sc-xx/archive/2011/12/03/2275084.html 这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 -- ...

  9. Oracle游标动态赋值

    1. oracle游标动态赋值的小例子 -- 实现1:动态给游标赋值 -- 实现2:游标用表的rowtype声明,但数据却只配置表一行的某些字段时,遍历游标时需fetch into到精确字段 CREA ...

随机推荐

  1. IOS测试程序运行耗时

    iOS设备相对于电脑,内存和处理能力有限,所以一段代码或者程序运行的时间需要时刻注意,这里提供两种获取精确时间的方法. 方法一:使用系统时间 NSDate* tmpStartData = [[NSDa ...

  2. Object C语法学习

    #synthesize关键字: 根据@property设置,自动生成成员变量相应的存取方法,从而可以使用点操作符来方便的存取该成员变量 . @implementation 关键字,表明类的实现 @en ...

  3. Face The Right Way---hdu3276(开关问题)

    题目链接:http://poj.org/problem?id=3276 题意:n牛头排成一排,每头牛两个状态,向前或向后,为了让所有的牛都向前,现在有一个机器 每次 能控制连续K头牛转换自己的状态,求 ...

  4. CodeTimer

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  5. Web设计者和开发者必备的28个Chrome插件

    摘要 对于许多Web设计者和开发者来说,Firefox浏览器是无法超越的,对于其他人Chrome正在蚕食Firefox的浏览器市场. 在过去的两年,谷歌Chrome浏览器的发布以来,引起了人们激烈争论 ...

  6. 简述Docker镜像、容器、仓库概念

    Docker镜像 Docker镜像(Image)类似于虚拟机的镜像,可以将他理解为一个面向Docker引擎的只读模板,包含了文件系统. 例如:一个镜像可以完全包含了Ubuntu操作系统环境,可以把它称 ...

  7. ionic环境搭建和安装

    1. 安装node环境 nodeJs环境的安装很简单,去官网下载最新版的NodeJs直接安装即可. Node官网: https://nodejs.org/ 安装完成后配置环境变量,计算机->属性 ...

  8. Json.Net Demo2

    新建一个空的Web项目,名称JsonServer,该网页实现Ajax数据请求和响应. 添加Newtonsoft.Json.dll的Dll引用,添加JQuery API文件,目录结构如下: 新建一个Pe ...

  9. MongoDB概念解析

    数据库 MongoDB默认把_id设置为主键(_开头的键是保留的) 数据库名必须为小写 RDBMS与MongoDB对应的术语比较 需注意 文档中的键/值对是有序的. 文档中的值不仅可以是在双引号里面的 ...

  10. glusterFS安装维护文档

    .规划: .依赖包 yum install libibverbs librdmacm xfsprogs nfs-utils rpcbind libaio liblvm2app lvm2-devel l ...