Oracle之PL/SQL游标练习题(三)
游标练习题目
- 1、定义游标:列出每个员工的姓名部门名称并编程显示第10个到第20个记录
- 2、定义游标:从雇员表中显示工资大于3000的记录,只要姓名、部门编号和工资,编程显示其中的奇数记录
- 3、用游标显示所有部门编号与名称,以及其所拥有的员工人数
- 4、用游标属性%rowcount实现输出前十个员工的信息
- 5、通过使用游标来显示dept表中的部门名称及其相应的员工列表(提示:可以使用双重循环)
- 6、接受一个部门号,使用For循环,从emp表中显示该部门的所有雇员的姓名,工作和薪水
- 7、编写一个程序块,将emp表中前5人的名字及其出的工资等级(salgrade)显示出来
- 8、emp表中对所有雇员按他们基本薪水的10%给他们加薪,如果所增加后的薪水大于5000,则取消加薪
- 9、按照salgrade表中的标准,给员工加薪,1:5%,2:4%,3:3%,4:2%,5:1%,并打印输出每个人加薪前后的工资
- 10、用游标获取所有收入超过2000的 salesman
1、定义游标:列出每个员工的姓名部门名称并编程显示第10个到第20个记录
法一:
declarecursor ed_cursor is select ename,dname from(select ename,dname,rownum r from emp e left join dept d on e.deptno=d.deptno)t where t.r between 10 and 20;v_ename varchar2(10);v_dname varchar2(10);
beginopen ed_cursor;fetch ed_cursor into v_ename,v_dname;while ed_cursor%found loopdbms_output.put_line(v_ename||' '||v_dname);fetch ed_cursor into v_ename,v_dname;end loop;close ed_cursor;
end;
法二:
declarecursor e_d is select * from(select e.ename,d.dname,rownum r from emp e,dept d where e.deptno=d.deptno)where r between 10 and 20;
beginfor e in e_d loopdbms_output.put_line(e.ename||','||e.dname);end loop;
end;
法三:
declarecursor e_d is select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno;
beginfor e in e_d loopif e_d%rowcount>=10 and e_d%rowcount<=20 thendbms_output.put_line(e.ename||','||e.dname);end if;end loop;
end;
2、定义游标:从雇员表中显示工资大于3000的记录,只要姓名、部门编号和工资,编程显示其中的奇数记录
法一:
declarecursor emp_cursor is select ename,deptno,sal from emp where sal>3000;v_ename varchar2(10);v_deptno number(10);v_sal number(10);
beginopen emp_cursor;fetch emp_cursor into v_ename,v_deptno,v_sal;while emp_cursor%found loopif emp_cursor%rowcount mod 2=1 thendbms_output.put_line(v_ename||' '||v_deptno||' '||v_sal);fetch emp_cursor into v_ename,v_deptno,v_sal;end if;end loop;close emp_cursor;
end;
法二:
declarecursor c_emp is select ename,deptno,sal from emp where sal>3000;
beginfor e in c_emp loopif mod(c_emp%rowcount,2)=1 thendbms_output.put_line(e.ename||','||e.deptno||','||e.sal);end if;end loop;
end;
3、用游标显示所有部门编号与名称,以及其所拥有的员工人数
法一:
declarecursor dept_cursor is select d.deptno,dname,count(*) from dept dleft join emp e on d.deptno=e.deptno group by d.deptno,dname;v_deptno number(20);v_dname varchar2(10);v_sum number(10);
beginopen dept_cursor;fetch dept_cursor into v_deptno,v_dname,v_sum;while dept_cursor%found loopdbms_output.put_line(v_deptno||' '||v_dname||' '||v_sum);fetch dept_cursor into v_deptno,v_dname,v_sum;end loop;close dept_cursor;
end;
法二:
declarecursor c_dept is select * from dept;v_n number:=0;
beginfor d in c_dept loopselect count(*) into v_n from emp where deptno=d.deptno;dbms_output.put_line(d.deptno||','||d.dname||',人数:'||v_n);end loop;
end;
4、用游标属性%rowcount实现输出前十个员工的信息
declarecursor c_emp is select * from emp;v_emp emp%rowtype;
beginopen c_emp;loopfetch c_emp into v_emp;exit when c_emp%notfound or c_emp%rowcount>10;dbms_output.put_line(v_emp.empno||','||v_emp.ename);end loop; close c_emp;
end;
5、通过使用游标来显示dept表中的部门名称及其相应的员工列表(提示:可以使用双重循环)
法一:
declarecursor dept_cursor is select dname,e.* from dept d left join emp e on e.deptno=d.deptno;v_dname varchar2(10);v_emp emp%rowtype;
beginopen dept_cursor;fetch dept_cursor into v_dname,v_emp.empno,v_emp.ename,v_emp.job,v_emp.mgr,v_emp.hiredate,v_emp.sal,v_emp.comm,v_emp.deptno;while dept_cursor%found loopdbms_output.put_line(v_dname||','||v_emp.empno||','||v_emp.ename||','||v_emp.job||','||v_emp.mgr||','||v_emp.hiredate||','||v_emp.sal||','||v_emp.comm||','||v_emp.deptno); fetch dept_cursor into v_dname,v_emp.empno,v_emp.ename,v_emp.job,v_emp.mgr,v_emp.hiredate,v_emp.sal,v_emp.comm,v_emp.deptno;end loop;close dept_cursor;
end;
法二:
declarecursor c_dept is select * from dept;--定义带参数的游标,根据提供的部门编号,去查询对应的员工列表cursor c_emp(dno emp.deptno%type) is select * from emp where deptno=dno;e emp%rowtype;
beginfor d in c_dept loopdbms_output.put_line('------部门名称:'||d.dname); open c_emp(d.deptno);--根据外层循环得到的部门编号去查询对应员工信息loopfetch c_emp into e;exit when c_emp%notfound;dbms_output.put_line(e.empno||','||e.ename);end loop; close c_emp;end loop;
end;
6、接受一个部门号,使用For循环,从emp表中显示该部门的所有雇员的姓名,工作和薪水
declarecursor emp_cursor is select * from emp where deptno=&deptno;
beginfor e in emp_cursor loopdbms_output.put_line(e.ename||','||e.job||','||e.sal);end loop;
end;
7、编写一个程序块,将emp表中前5人的名字及其出的工资等级(salgrade)显示出来
法一:
declarecursor es_cursor is select ename,sal,gradefrom(select ename,sal,rownum r from emp)e,salgrade swhere r<=5 and sal between losal and hisal;v_ename varchar2(10);v_sal number(10,2);v_grade number(2);
beginopen es_cursor;fetch es_cursor into v_ename,v_sal,v_grade;while es_cursor%found loopdbms_output.put_line(v_ename||', '||v_sal||', '||v_grade);fetch es_cursor into v_ename,v_sal,v_grade;end loop;close es_cursor;
end;
法二:
declarecursor c_e is select e.ename,s.grade,rownum from emp e,salgrade swhere e.sal between s.losal and s.hisal and rownum<=5;
beginfor e in c_e loopdbms_output.put_line(e.ename||','||e.grade); end loop;
end;
8、emp表中对所有雇员按他们基本薪水的10%给他们加薪,如果所增加后的薪水大于5000,则取消加薪
declarecursor cemp is select * from emp;
beginfor e in cemp loopif e.sal*1.1<=5000 thenupdate emp set sal=sal*1.1 where empno=e.empno;end if;end loop;
end;
9、按照salgrade表中的标准,给员工加薪,1:5%,2:4%,3:3%,4:2%,5:1%,并打印输出每个人加薪前后的工资
法一:
declarecursor es_cursor is select ename,sal,grade from emp e,salgrade s where sal between losal and hisal;v_ename varchar2(10);v_sal number(10,2);v_grade number(2);
beginopen es_cursor;fetch es_cursor into v_ename,v_sal,v_grade;while es_cursor%found loopdbms_output.put_line('加薪前: '||v_ename||', '||v_sal||', '||v_grade);case v_grade when 1 then v_sal:=v_sal*1.05;when 2 then v_sal:=v_sal*1.04;when 3 then v_sal:=v_sal*1.03;when 4 then v_sal:=v_sal*1.02;when 5 then v_sal:=v_sal*1.01;end case;dbms_output.put_line('加薪后: '||v_ename||', '||v_sal);update emp set sal=v_sal where ename=v_ename;fetch es_cursor into v_ename,v_sal,v_grade;end loop;close es_cursor;
end;
法二:
declarecursor emp_sal_cursor is select sal,empno from emp;v_sal emp.sal%type;v_id emp.empno%type;
beginopen emp_sal_cursor;fetch emp_sal_cursor into v_sal,v_id;while emp_sal_cursor%found loopdbms_output.put_line('加薪前: '||v_id || ': ' || v_sal);if v_sal between 700 and 1200 then v_sal:=v_sal+v_sal*0.05;elsif v_sal between 1201 and 1400 then v_sal:=v_sal+v_sal*0.04;elsif v_sal between 1401 and 2000 then v_sal:=v_sal+v_sal*0.03;elsif v_sal between 2001 and 3000 then v_sal:=v_sal+v_sal*0.02;else v_sal:=v_sal+v_sal*0.01;end if;update emp set sal=v_sal where empno=v_id;dbms_output.put_line('加薪后: '||v_id || ': ' || v_sal);fetch emp_sal_cursor into v_sal,v_id;end loop;close emp_sal_cursor;
end;
法三:
declarecursor emp_sal_cursor is select sal,empno from emp;temp number(4,2);
beginfor c in emp_sal_cursor loopdbms_output.put_line(c.empno||': '||c.sal);if c.sal between 700 and 1200 then c.sal:=c.sal+c.sal*0.05;elsif c.sal between 1201 and 1400 then c.sal:=c.sal+c.sal*0.04;elsif c.sal between 1401 and 2000 then c.sal:=c.sal+c.sal*0.03;elsif c.sal between 2001 and 3000 then c.sal:=c.sal+c.sal*0.02;else c.sal:=c.sal+c.sal*0.01;end if;dbms_output.put_line(c.empno||': '||c.sal);update emp set sal=c.sal where empno=c.empno;end loop;
end;
法四:
declarecursor c_e is select e.empno,e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal;sal_1 emp.sal%type;--加薪前的工资sal_2 emp.sal%type;--加薪后的工资
beginfor e in c_e loopsal_1:=e.sal;case e.gradewhen 1 thensal_2:=e.sal*1.05;when 2 thensal_2:=e.sal*1.04;when 3 thensal_2:=e.sal*1.03;when 4 thensal_2:=e.sal*1.02;when 5 thensal_2:=e.sal*1.01;end case;update emp set sal=sal_2 where empno=e.empno;dbms_output.put_line(e.empno||','||e.ename||','||sal_1||','||sal_2);end loop;
end;
10、用游标获取所有收入超过2000的 salesman
法一:
declarecursor emp_cursor is select * from emp where (sal+nvl(comm,0))>2000 and job='SALESMAN';v_emp emp%rowtype;
beginopen emp_cursor;fetch emp_cursor into v_emp;while emp_cursor%found loopdbms_output.put_line(v_emp.empno||','||v_emp.ename);fetch emp_cursor into v_emp;end loop;close emp_cursor;
end;
法二:
declarecursor c_emp is select * from emp where (sal+nvl(comm,0))>2000 and job=upper('salesman');
beginfor e in c_emp loopdbms_output.put_line(e.ename);end loop;
end;
相关文章:

Oracle之PL/SQL游标练习题(三)
游标练习题目1、定义游标:列出每个员工的姓名部门名称并编程显示第10个到第20个记录2、定义游标:从雇员表中显示工资大于3000的记录,只要姓名、部门编号和工资,编程显示其中的奇数记录3、用游标显示所有部门编号与名称,…...

docker运行服务端性能监控系统Prometheus和数据分析系统Grafana
文章目录一、Prometheus的安装和运行1、使用docker拉取镜像2、创建prometheus.yml文件3、启动容器4、查看启动是否成功5、记录安装过程中出现的错误二、Grafana的安装和运行1、使用docker拉取镜像2、创建grafana3、运行grafana4、查看grafana运行日志5、登录grafana一、Prometh…...

【Linux】【应用层】多线程编程
一、线程创建 Linux 中的 pthread_create() 函数用来创建线程,它声明在<pthread.h>头文件中,语法格式如下: int pthread_create(pthread_t *thread,const pthread_attr_t *attr,void *(*start_routine) (void *),void *arg);各个参数…...

GameFramework 框架详解之 如何接入热更框架HybridCLR
一.前言 HybridCLR是一个特性完整、零成本、高性能、低内存的近乎完美的c#热更新方案 GameFramework是一个非常出色完整的基于Unity引擎的游戏框架,里面包含了非常多的模块,封装非常完整。 以前市面上的热更大多数都是Lua为主,后来出了一个ILRuntime的C#热更框架,虽然性能…...

全国青少年软件编程(Scratch)等级考试二级考试真题2023年3月——持续更新.....
一、单选题(共25题,共50分) 1. 小猫的程序如图所示,积木块的颜色与球的颜色一致。点击绿旗执行程序后,下列说法正确的是?( ) A.小猫一直在左右移动,嘴里一直说着“抓到了”。 B.小猫会碰到球,然后停止。 C.小猫一直在左右移动,嘴里一直说着“别跑” D.小猫会碰到球,…...

HTML2.1列表标签
列表标签种类 无序列表 有序列表 自定义列表 使用场景:在列表中按照行展示关联性内容。 特点:按照行的形式,整齐显示内容。 一、无序列表 标签名说明ul无序列表整体,用于包裹li标签li表示无序列表的每一项,用于包…...

在 Flutter 多人视频通话中实现虚拟背景、美颜与空间音效
前言 在之前的「基于声网 Flutter SDK 实现多人视频通话」里,我们通过 Flutter 声网 SDK 完美实现了跨平台和多人视频通话的效果,那么本篇我们将在之前例子的基础上进阶介绍一些常用的特效功能,包括虚拟背景、色彩增强、空间音频、基础变声…...

Ambari-web 架构
Ambari-web 使用的前端 Embar.js MVC 框架实现,Embar.js 是一个 TodoMVC 框架,涵盖了单页面应用(single page application)几乎所有的行为 Nodejs 是一个基于 Chrome JavaScript 运行时建立的一个平台,用来方便的搭建…...

对接百思买Best Buy EDI 的注意事项
在此前的文章:《Best Buy Drop Ship(Commerce hub) EDI业务测试常见报错及解决》中,我们介绍了在业务测试过程中遇到的常见报错及解决方案,以下在此基础上进行补充。 数据未能成功发送给Best Buy可能遇到的情况 Best Buy EDI项目传输业务报…...

2023年郑州重点建设项目名单公布,中创“算力数据中心”项目入选!
4月7日,郑州市人民政府网站公布2023年郑州市重点建设项目名单,名单共列项目680个,总投资1.08万亿元,年度计划投资2691亿元。 在创新驱动能力提升项目名单里,中创算力与人民网人民数据(国家大数据灾备中心&a…...

Pytorch 容器 - 1. Module类介绍
目录 1. 基于Module构建自己的网络 2. Module的初始化变量 3. Modules中需要子类 forward() 4. Modules中其他内置函数 1. 基于Module构建自己的网络 torch.nn.Module是所有神经网络模块的基类,如何定义自已的网络: 由于 Module 是神经网络模块的基…...

百度墨卡托坐标转化笔记
一、墨卡托坐标转化 调研了python和java多种实现方式的转换,发现有的不符合需求,原因还没找到。 我是用百度地图返回的poi边界(返回的是墨卡托坐标) 转换的原理没有深入研究,直接拿来用的,测试可行&…...

每日学术速递4.12
CV - 计算机视觉 | ML - 机器学习 | RL - 强化学习 | NLP 自然语言处理 Subjects: cs.HC 随着新的“生成代理”论文的发布,LLM刚刚达到了一个重要的里程碑——通过使用 LLM,生成代理能够在受《模拟人生》启发的交互式沙箱中模拟类人行为。代理架构扩展…...

HarmonyOS/OpenHarmony公司级技术开发团队硬件基本配置清单
有朋友公司咨询进入HarmonyOS/OpenHarmony领域,组建技术团队,硬件设备的基本配置应该是怎么样的比较合适?这个是进入鸿蒙开发领域相关配置的第一步,我们以一个基本的团队配置为例说明,供想进入的团队参考。 HarmonyOS/…...

新一代信息技术赋能,安科瑞搭建智慧水务体系的新思路
随着新时期治水方针的逐步落实,水利现代化、智能化建设已开启,物联网、图像识别、数字孪生等新技术的成熟,也为智慧水务体系的搭建提供了技术保障,新时代治水新思路正逐步得到落实。本文对智慧水务的总体架构与包含的建设内容进行…...

37岁测试工程师被裁,120天没找到工作,无奈...
从短期来看,程序员的确算是个不错的工作,薪水也比一般岗位高很多,但是从长远来看,程序员的中年危机会比其他岗位来的更早,很多程序员只有到了35岁左右,才能真正认清楚互联网行业,尤其是被裁之后…...

Java容器使用注意点
前置:问题 判空集合转map集合遍历集合去重集合转数组数组转集合 一:集合判空 《阿里巴巴 Java 开发手册》的描述如下: 判断所有集合内部的元素是否为空,使用 isEmpty() 方法,而不是 size()0 的方式。 我们在开发中也…...

密文题解(图论+字典树)
题目大意 有一段长度为nnn的密文,密文的每一位都可以用一个非负整数来描述,并且每一位都有一个权值aia_iai。你可以操作任意多次,每次操作可以选择任意一段密文,花费选择的所有位上权值的异或和的代价获得这段密文每一位的异或…...

Baumer工业相机堡盟工业相机如何通过BGAPISDK里的工具函数来计算工业相机的实时帧率(C#)
Baumer工业相机堡盟工业相机如何通过BGAPISDK里函数来计算相机的实时帧率(C#)Baumer工业相机Baumer工业相机的帧率的技术背景Baumer工业相机的帧率计算方式在BufferEvent声明显示FrameID设计显示帧率的函数Baumer工业相机通过BGAPI SDK计算帧率的优势B…...

数据结构与常量(Java)
目录 1.字面常量 2. 数据类型 3. 变量 3.1 变量概念 3.2 语法格式 补充:变量 int long short double和float char boolean byte 4.类型转换 类型提升小结 5. 字符串类型 1. int 转成 String 2. String 转成 int 1.字面常量 类似System.Out.p…...

【LeetCode】剑指 Offer 54. 二叉搜索树的第k大节点 p269 -- Java Version
题目链接:https://leetcode.cn/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/ 1. 题目介绍( 54. 二叉搜索树的第k大节点) 给定一棵二叉搜索树,请找出其中第 k 大的节点的值。 【测试用例】: 示例 1: 示例2&…...

[工具类] post请求 获取request对象, 获取request的请求体(body)参数
目录 引言: 1. 获取request对象的几种常用方式 -> 1.1 获取请求对象 通过请求上下文对象 获取信息[推荐] -> 1.2 在controller层直接获取[不推荐 侵害性太强] -> 1.3 interceptor中获取[部分业务中使用] -> 1.4 request常用api简介 2. 获取request的body的工具…...

Golang 多版本安装小工具G
voidint制作的Golang版本安装管理,非常好用。想装就装,想换版本就换版本 除了一些使用go install的场景可能有不兼容,主要是安装了工具有时候不能直接用。 GitHub - voidint/g: Golang Version Manager 使用方式很简单&a…...

day29—选择题
文章目录1.HashSet子类依靠什么方法区分重复元素(C)2.以下代码在编译和运行过程中会出现什么情况(A)3.有这么一段程序,执行的结果是(C)1.HashSet子类依靠什么方法区分重复元素(C&…...

day8 互斥锁/读写锁的概念及使用、死锁的避免
目录 互斥锁的概念和使用 线程通信 - 互斥 互斥锁的创建和销毁 互斥锁的创建 互斥锁的销毁 互斥锁的使用 申请锁 释放锁 互斥锁的概念和使用 线程通信 - 互斥 临界资源: 一次只允许一个任务(进程、线程)访问的共享资源;…...

2023-04-13 monetdb-str类型变长存储-分析
摘要: monetdb的列的基本抽象是BAT,但是对于列数据的存储方式, 对于固定长度和不固定长度,使用了不同的存储方式。 固定长度的数据比如int,int64之类的, 直接存储在了数据tail文件。 但是对于不固定长度比如string, 则使用另外一个独立的theap文件存储, tail文件仅保留对于…...

011:Mapbox GL两种方式隐藏logo和版权,个性化版权的声明
第011个 点击查看专栏目录 本示例的目的是介绍演示如何在vue+mapbox中用两种方式隐藏logo和版权,并个性化版权的声明 。 直接复制下面的 vue+mapbox源代码,操作2分钟即可运行实现效果 文章目录 示例效果配置方式示例源代码(共91行)相关API参考:专栏目标示例效果 配置方式…...

结合PCA降维的DBSCAN聚类方法(附Python代码)
目录 前言介绍: 1、PCA降维: (1)概念解释: (2)实现步骤: (3)优劣相关: 2、DBSCAN聚类: (1)概念解释&a…...

限流:计数器、漏桶、令牌桶 三大算法的原理与实战(史上最全)
限流 限流是面试中的常见的面试题(尤其是大厂面试、高P面试) 注:本文以 PDF 持续更新,最新尼恩 架构笔记、面试题 的PDF文件,请到文末《技术自由圈》公号获取 为什么要限流 简单来说: 限流在很多场景中用来…...

Redis用于全局ID生成器、分布式锁的解决方案
全局ID生成器 每个店铺都可以发布优惠卷 当用户抢购时,就会生成订单并保存到tb_voucher_order这张表中,而订单表如果使用数据库自增id就存在一些问题: 1.id的规律性太明显 2.受单表数据量的限制 全局ID生成器,是一种在分布式系…...