watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGFuZ2xpdXFpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

表、select语句、游标:返回结果都能是一个集合。

注意:游标的结果是一个集合。

--查询并打印员工的姓名和薪水
set serveroutput on
/*
光标:
1. 光标的属性: %isopen %rowcount(返回的行数)
%notfound %found 2. 默认情况下。一次性打开300个光标
SQL> show parameter cursor NAME TYPE VALUE
------------------------------------ ----------- ---------------
cursor_sharing string EXACT
cursor_space_for_time boolean FALSE
open_cursors integer 300
session_cached_cursors integer 20 改动光标:
alter system set open_cursors=400;
*/
declare
--定义一个光标
cursor cemp is select ename,sal from emp;
pename emp.ename%type;引用变量
psal emp.sal%type;引用变量
begin
open cemp;
loop
--取一条记录
fetch cemp into pename,psal;
--退出: fetch没有取到
exit when cemp%notfound; dbms_output.put_line(pename||'的薪水是'||psal); end loop;
close cemp;
end;
/
--查询某个部门的员工姓名
set serveroutput on declare
cursor cemp(dno number) is select ename from emp where deptno=dno;
pename emp.ename%type;
begin
open cemp(20);
loop
fetch cemp into pename;
exit when cemp%notfound; dbms_output.put_line(pename); end loop;
close cemp;
end;
/
/*
SQL语句
select to_char(hiredate,'YYYY') from emp
--> 光标 --> 循环 --> 退出条件 变量:
count80 number := 0;
count81 number := 0;
count82 number := 0;
count87 number := 0;
*/
set serveroutput on
declare
cursor cemp is select to_char(hiredate,'YYYY') from emp;
phiredate varchar2(4); count80 number := 0;
count81 number := 0;
count82 number := 0;
count87 number := 0;
begin
open cemp;
loop
--取一个员工的年份
fetch cemp into phiredate;
exit when cemp%notfound; --推断年份
if phiredate = '1980' then count80 := count80+1;
elsif phiredate = '1981' then count81 := count81+1;
elsif phiredate = '1982' then count82 := count82+1;
else count87 := count87 + 1;
end if;
end loop;
close cemp; --输出
dbms_output.put_line('Total:'||(count80+count81+count82+count87));
dbms_output.put_line('1980:'||count80);
dbms_output.put_line('1981:'||count81);
dbms_output.put_line('1982:'||count82);
dbms_output.put_line('1987:'||count87);
end;
/
/*
SQL语句:
select empno,sal from emp order by sal
--> 光标 --> 循环 --> 退出条件: 1. 总额> 5w 2. 全部人涨完 变量:
涨工资的人数: countEmp number := 0;
涨后的工资总额: salTotal number;
1. select sum(sal) into salTotal from emp;
2. 涨后 = 涨前 + sal * 0.1 练习: 工资总额不能超过5w
*/
set serveroutput on
declare
cursor cemp is select empno,sal from emp order by sal;
pempno emp.empno%type;
psal emp.sal%type;
--涨工资的人数:
countEmp number := 0; --涨后的工资总额:
salTotal number;
begin
--得到工资初始值
select sum(sal) into salTotal from emp;
open cemp;
loop
--1. 总额> 5w
exit when salTotal > 50000;
--取一个员工
fetch cemp into pempno,psal;
--2. 全部人涨完
exit when cemp%notfound; --涨工资
update emp set sal=sal*1.1 where empno=pempno;
--人数+1
countEmp := countEmp +1;
--总额
salTotal := salTotal + psal * 0.1;
end loop;
close cemp; commit;
dbms_output.put_line('人数:'||countEmp||' 涨后的工资总额:'||salTotal);
end;
/
/*
SQL语句:
部门: select deptno from dept --> 光标 --> 循环 --> 退出条件
部门中员工的薪水: select sal from emp where deptno=??
--> 带參数的光标 --> 循环 --> 退出条件 变量:
每一个段的人数:
count1 number; count2 number; count3 number;
部门的工资总额:
salTotal number;
1. 累加
2. select sum(sal) into salTotal from emp where deptno=? ? */
set serveroutput on
declare
--部门
cursor cdept is select deptno from dept;
pdeptno dept.deptno%type; --部门中员工的薪水
cursor cemp(dno number) is select sal from emp where deptno=dno;
psal emp.sal%type; --每一个段的人数:
count1 number; count2 number; count3 number; --部门的工资总额:
salTotal number;
begin
open cdept;
loop
--取一个部门
fetch cdept into pdeptno;
exit when cdept%notfound;
--初始化
count1:=0; count2:=0; count3:=0;
--部门的工资总额
select sum(sal) into salTotal from emp where deptno=pdeptno;
--部门中员工的薪水
open cemp(pdeptno);
loop
-- 取一个员工的薪水
fetch cemp into psal;
exit when cemp%notfound; --推断薪水
if psal < 3000 then count1:=count1+1;
elsif psal>=3000 and psal< 6000 then count2:=count2+1;
else count3:=count3+1;
end if; end loop;
close cemp; --保存当前部门
insert into msg values(pdeptno,count1,count2,count3,nvl(salTotal,0)); end loop;
close cdept;
commit; dbms_output.put_line('完成');
end;
/

Oracle游标(光标)的更多相关文章

  1. [转载]oracle游标概念讲解

    原文URL:http://www.2cto.com/database/201203/122387.html ORACLE游标概念讲解 什么是游标?  ①从表中检索出结果集,从中每次指向一条记录进行交互 ...

  2. Oracle 游标使用总结(好文章)

    游标(CURSOR)也叫光标,在关系数据库中经常使用,在PL/SQL程序中可以用CURSOR与SELECT一起对表或者视图中的数据进行查询并逐行读取. Oracle游标分为显示游标和隐式游标. 显示游 ...

  3. ORACLE游标概念讲解

    1,什么是游标? ①从表中检索出结果集,从中每次指向一条记录进行交互的机制. ②关系数据库中的操作是在完整的行集合上执行的.   由 SELECT 语句返回的行集合包括满足该语句的 WHERE 子句所 ...

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

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

  5. Oracle游标带参数

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

  6. Oracle 游标使用(转)

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

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

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

  8. Oracle游标动态赋值

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

  9. dapper支持oracle游标

    dapper支持oracle游标 Dapper是一个轻型的ORM类.它有啥优点.缺点相信很多朋友都知道了,园里也有很多朋友都有相关介绍,这里就不多废话. 如果玩过Oracle都知道,存储过程基本都是通 ...

  10. Oracle游标的使用示例

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

随机推荐

  1. python socket编程实现的简单tcp迭代server

    与c/c++ socket编程对照见http://blog.csdn.net/aspnet_lyc/article/details/38946915 server: import socket POR ...

  2. Do you master on array in C ?

    Do you master on array in C ? 因为新标准C99的支持变长数组, 差点儿C的标准特性就是看着gcc来的(Linux 内核严重依赖GCC) int mani() { cons ...

  3. HDU 5071 Chat

    题意: CLJ找了很多妹子-  (题目好没节操-)  对于CLJ和妹子的聊天对话框  有一下几种操作: add  加一个妹子在聊天窗队列末尾  假设这个妹子已经在队列中则add失败 close  关掉 ...

  4. hdu2159(二维完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 题意:打怪,还有最后一级,忍耐度有限m,问在杀怪数量上限为s的情况下能否获取n经验而通关,且忍耐 ...

  5. hdu1267(递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1267 题意:假定一个字符串由m个H和n个D组成,从左到右扫描该串,如果字符H的累计数总是不小于字符D的 ...

  6. codeforces#253 D - Andrey and Problem里的数学知识

    这道题是这种,给主人公一堆事件的成功概率,他仅仅想恰好成功一件. 于是,问题来了,他要选择哪些事件去做,才干使他的想法实现的概率最大. 我的第一个想法是枚举,枚举的话我想到用dfs,但是认为太麻烦. ...

  7. ftk学习记(waitbox篇)

    [声明:版权全部.欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 前面说到了脚本.那么就看看ftk中demo与script搭配的效果是什么样的? 上面的效果图就相 ...

  8. extjs4 分页工具栏pagingtoolbar的每页显示数据combobox下拉框

    var itemsPerPage = 20; var combo; //创建数据源store Ext.define('recordStore', { extend : 'Ext.data.Store' ...

  9. struts(二)——struts框架实现的基本原理

    上一篇文章,我们介绍了MVC框架的基本原理,并指出了这个基本框架中存在大量if…else的问题.今天我们来介绍一下struts框架,让struts解决这个问题. 首先,看一下粗略的时序图: Actio ...

  10. 实现Timeline

    Redis实现Timeline 上回写了使用Redis实现关注关系,这次说说使用Redis实现Timeline. Timeline的实现一般有推模式.拉模式.推拉结合这几种.推模式:某人发布内容之后推 ...