PLSQL Day4

--使用显式游标更新行,对所有salesman增加500奖金:
declare
  cursor s_cursor is
  select * from emp 
  where job = 'SALESMAN'
  for update;
begin
  for e_s in s_cursor loop
    update emp set comm = nvl(comm,0)+500 
    where current of s_cursor;
  end loop;
end;

--3.定义游标:显示所有部门编号与名称,以及其所拥有的员工人数:
declare
  cursor i_cursor is
  select d.deptno,d.dname,count(e.empno) cnt
  from dept d  left join emp e
  on d.deptno = e.deptno
  group by d.deptno,d.dname;
begin
  for e in i_cursor loop
    dbms_output.put_line(e.deptno||' '||e.dname||' '||e.cnt);
  end loop;
end;

--4.用游标属性%rowcount实现输出前十个员工的信息:

declare
  cursor a_cursor is
  select * from emp;
begin
  for e in a_cursor loop
    if a_cursor%rowcount < 11 
       then dbms_output.put_line(e.empno||' '||e.ename||' '||e.job
         ||' '||e.mgr||' '||to_char(e.hiredate,'yyyy-MM-dd')
         ||' '||e.sal||' '||e.comm
         ||' '||e.deptno);
    end if;
  end loop;
end;
--5.通过使用游标来显示dept表中的部门名称,
--及其相应的员工列表(提示:可以使用双重循环):
declare
  cursor a_cur is select * from dept;
  cursor b_cur is select * from emp;
begin
  for a in a_cur loop
    for b in b_cur loop
      if a.deptno=b.deptno then 
        dbms_output.put_line(a.dname||' '||b.empno||' '
        ||b.ename||' '||b.job||' '||b.mgr||
        ' '||to_char(b.hiredate,'yyyy-mm-dd')||' '
        ||b.sal||' '||b.comm);
      end if;
    end loop;
  end loop;
end;

--6.定义游标:接受一个部门号,从emp表中显示该部门的所有雇员的姓名,工作和薪水:
declare
  cursor c_cur(dno number) is select * from emp where deptno = dno;
begin
  for e in c_cur(&部门号) loop
    dbms_output.put_line(e.ename||' '||e.job||' '||e.sal);
  end loop;
end;

--7.定义游标:将emp表中前5人的名字,及其工资等级(salgrade)显示出来:
declare
  cursor d_cur is select e.ename,s.grade 
  from emp e join salgrade s
  on e.sal between s.losal and s.hisal;
begin
  for e in d_cur loop
    if d_cur%rowcount < 6
      then dbms_output.put_line(e.ename||' '||e.grade)  ;  
    end if;
  end loop;
end;


--8.定义游标:在emp表中对所有雇员按他们基本薪水的10%给他们加薪,
--如果所增加后的薪水大于5000,则取消加薪:

declare
  cursor e_cur is select * from emp for update;
begin
  for e in e_cur loop    
    e.sal := e.sal*1.1;
    if e.sal<=5000 then
       update emp set sal = sal*1.1 where current of e_cur;
    end if;
  end loop;
end;

select * from emp;
--9.按照salgrade表中的标准,给员工加薪,
--1:5%,2:4%,3:3%,4:2%,5:1%,
--并打印输出每个人,加薪前后的工资:

declare
  cursor f_cur is 
  select e.empno,e.ename,e.sal,s.grade 
  from emp e join salgrade s
  on e.sal between s.losal and s.hisal
  for update;
  e_sal number;
begin
  for e in f_cur loop
    if e.grade = 1 
    then e_sal := e.sal*1.05  ;        
    elsif e.grade = 2 
    then e_sal := e.sal*1.04  ;  
    elsif e.grade = 3 
    then e_sal := e.sal*1.03  ; 
    elsif e.grade = 4 
    then e_sal := e.sal*1.02  ;
    else e_sal := e.sal*1.01  ; 
    end if;
    dbms_output.put_line(e.ename||' - 前:'||e.sal||'  后:'||e_sal);
    update emp set sal = e_sal  
    where empno = e.empno;
  end loop;
end;

select * from emp;
select * from salgrade;


-----

declare
  cursor f_cur is select * from emp for update;
  cursor g_cur is select * from salgrade;
  e_sal number;  
begin
  for f in f_cur loop
    for g in g_cur loop
      if f.sal between g.losal and g.hisal and g.grade = 1
      then e_sal := f.sal * 1.05;
      elsif f.sal between g.losal and g.hisal and g.grade = 2
      then e_sal := f.sal * 1.04;   
      elsif f.sal between g.losal and g.hisal and g.grade = 3
      then e_sal := f.sal * 1.03;  
      elsif f.sal between g.losal and g.hisal and g.grade = 4
      then e_sal := f.sal * 1.02;   
      elsif f.sal between g.losal and g.hisal and g.grade = 5
      then e_sal := f.sal * 1.01; 
      end if; 
    end loop;
    dbms_output.put_line(f.ename||' - 前:'||f.sal||'  后:'||e_sal);
    update emp set sal = e_sal  where current of f_cur;
  end loop;
end;


--10.用游标获取所有收入(sal+comm)超过2000的 salesman:

declare
  cursor h_cur is select * from emp where sal+nvl(comm,0)>2000 and job = 'SALESMAN';
begin
  for h in h_cur loop
    dbms_output.put_line(h.empno||' '
        ||h.ename||' '||h.job||' '||h.mgr||
        ' '||to_char(h.hiredate,'yyyy-mm-dd')||' '
        ||h.sal||' '||h.comm||' '||h.deptno);
  end loop;
end;
--11.定义游标:按工号从小到大的顺序输出雇员名字、工资以及工资与所在部门平均工资的差额:

declare
  cursor i_cur is 
  select e.ename,e.sal,e.sal-t.avg c from emp e,
  (select deptno,round(avg(sal),2) avg from emp group by deptno) t 
  where t.deptno = e.deptno order by e.sal;
begin
  for i in i_cur loop
    dbms_output.put_line(i.ename||' '
        ||i.sal||' '||i.c);
  end loop;
end;

--12.定义游标:以提升两个资格最老的‘职员’(CLERK)为‘高级职员’(HIGHCLERK):(工作时间越长,优先级越高)

declare
  cursor j_cur is 
  select * from emp where job = 'CLERK' order by hiredate
  for update;
begin
  for j in j_cur loop
    if j_cur%rowcount < 3
      then
        update emp set job = 'HIGHCLERK' where current of j_cur;
    end if;
  end loop;
end;

select * from emp;
--13.使用显式游标更新行,删除薪资最低的那个员工:
declare
  cursor k_cur is select * from emp for update;
  min_sal number;
begin
  select min(sal) into min_sal from emp;
  for k in k_cur loop
    if k.sal = min_sal then
       delete from emp where current of k_cur;
    end if;
  end loop;
end;

----
declare
  cursor k_cur is select * from emp order by sal for update;
begin
  for k in k_cur loop
    if k_cur%rowcount = 1 then
       delete from emp where current of k_cur;
    end if;
  end loop;
end;

with soucre as ( 
    select 1 as id , 3 as score from dual
    union all 
    select 2 as id , 4 as score from dual
    union all 
    select 3 as id , null as score from dual
    union all 
    select 4 as id , 3 as score from dual
    union all 
    select 5 as id , null as score from dual
    union all 
    select 6 as id , null as score from dual
    union all 
    select 7 as id , 5 as score from dual) 
select t.id,
nvl(t.score,lag(t.score)over(order by t.id)) score 
from (
select s.id,
nvl(s.score,lag(s.score)over(order by s.id)) score 
from soucre s)t
    
-- 测试数据表创建
with soucre as ( 
    select 1 as id , 3 as score from dual
    union all 
    select 2 as id , 4 as score from dual
    union all 
    select 3 as id , null as score from dual
    union all 
    select 4 as id , 3 as score from dual
    union all 
    select 5 as id , null as score from dual
    union all 
    select 6 as id , null as score from dual
    union all 
    select 7 as id , 5 as score from dual) -- 测试数据表创建
select id,score,nvl(score,lag(score ignore nulls) over(order by id)) a from soucre;

/*create table customer(
cust_id number
,certificate_no char(18));
create table application(
apply_id number
,cust_id number
,amount number);
insert into customer values(1,370284199611045316);
insert into customer values(2,370284198011045316);
insert into customer values(3,370284196511045316);
insert into application values(11,1,700);
insert into application values(12,2,500);
insert into application values(13,3,200);*/
select * from customer;
select* from application;

select nvl(区间,'总计')区间,count(cust_id) 总人数,count(apply_id) 交易笔数,sum(amount)交易总金额 from(
select case when months_between(sysdate,d)/12 between 0 and 30 then '0-30岁' 
            when months_between(sysdate,d)/12 > 30 and months_between(sysdate,d)/12 <= 50 then '30-50岁'
            when months_between(sysdate,d)/12 > 50 then '50岁以上' end 区间,
cust_id,apply_id,amount from
(with t as
(select c.*,a.amount,a.apply_id from customer c,application a 
where a.cust_id = c.cust_id)
select cust_id,apply_id,to_date(substr(t.certificate_no,7,8),'yyyy-MM-dd') d,amount from t)
)group by rollup(区间);

------
with t as (select cust_id,apply_id,amount ,
            case when age between 0 and 30 then '0-30岁' 
            when age > 30 and age <= 50 then '30-50岁'
            when age > 50 then '50岁以上' end 区间
from (select c.cust_id,months_between(sysdate,to_date(substr(certificate_no,7,8),'yyyy-MM-dd'))/12 age,a.amount,a.apply_id 
from customer c,application a 
where a.cust_id = c.cust_id) 
)
select nvl(区间,'总计')区间,count(cust_id) 总人数,count(apply_id) 交易笔数,sum(amount)交易总金额 from t group by rollup(区间);
-----
with ca as
(select cust_id,amount,apply_id,
case when year between 0 and 30 then '0-30岁' 
     when year between 30 and 50 then '30-50岁'
     when year > 50 then '50岁以上'end age
from (select a.*,
to_char(sysdate,'yyyy')-substr(certificate_no,7,4) year
from customer c
join application a on c.cust_id=a.cust_id))
select nvl(age,'总计') 区间,count(cust_id) 总人数,count(*) 交易笔数,sum(amount) 交易总金额 
from ca group by rollup(age);


------------------------------------------------------------------------------------------------------------------

declare
  v_emp emp_bak%rowtype;
begin
  update emp_bak set comm=100 where deptno=&deptno;
  dbms_output.put_line('修改的数据条数:'||sql%rowcount);
  if sql%found then
    dbms_output.put_line('aaaaaaaaaaaaaaa');
  end if;
  delete from emp_bak where deptno=&dno;
  dbms_output.put_line('删除了'||sql%rowcount||'条数据'); 
end;

select * from emp_bak;
 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/42589.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AFT:Attention Free Transformer论文笔记

原文链接 2105.14103 (arxiv.org) 原文翻译 Abstract 我们介绍了 Attention Free Transformer (AFT)&#xff0c;这是 Transformer [1] 的有效变体&#xff0c;它消除了点积自注意力的需要。在 AFT 层&#xff0c;键key和值value首先与一组学习的位置偏差position biases相结…

ubuntu22安装Docker并配置

安装Docker sudo apt install docker.io使用脚本自动安装docker&#xff1a; curl -fsSL get.docker.com -o get-docker.sh sudo sh get-docker.sh --mirror Aliyun配置国内镜像 /etc/docker/daemon.json 推荐配置&#xff1a; {"registry-mirrors": ["htt…

Lab1 论文 MapReduce

目录 &#x1f339;前言 &#x1f985;2 Programming Model &#x1f33c;2.1 Example &#x1f33c;2.2 Types &#x1f33c;2.3 More Examples &#x1f985;3 Implementation(实现) &#x1f33c;3.1 ~ 3.3 &#x1f33c;3.4 ~ 3.6 &#x1f985;4 Refinemen…

代理IP有什么用途

代理IP主要有以下应用场景&#xff1a; 1、隐藏真实IP地址&#xff1a;通过使用代理IP&#xff0c;可以隐藏真实的网络请求来源&#xff0c;保护用户隐私。 2、绕过网络限制&#xff1a;一些地区或网络环境可能存在访问限制&#xff0c;通过使用代理IP可以绕过这些限制&#xf…

Anaconda+Pycharm 项目运行保姆级教程(附带视频)

最近很多小白在问如何用anacondapycharm运行一个深度学习项目&#xff0c;进行代码复现呢&#xff1f;于是写下这篇文章希望能浅浅起到一个指导作用。 附视频讲解地址&#xff1a;AnacondaPycharm项目运行实例_哔哩哔哩_bilibili 一、项目运行前的准备&#xff08;软件安装&…

BN的 作用

1、背景&#xff1a; 卷积神经网络的出现&#xff0c;网络参数量大大减低&#xff0c;使得几十层的深层网络成为可能。然而&#xff0c;在残差网络出现之前&#xff0c;网络的加深使得网络训练变得非常不稳定&#xff0c;甚至出现网络长时间不更新或者不收敛的情形&#xff0c;…

ER模型理论和三范式

ER模型理论和三范式 各种关系多对一一对一一对多多对多 三范式理论函数依赖完全函数依赖部分函数依赖传递&#xff08;间接&#xff09;函数依赖 第一范式&#xff1a;属性&#xff08;表字段&#xff09;不可切割第二范式&#xff1a;不能存在 部分函数依赖(都存在完全函数依赖…

2款一键word生成ppt的AI工具,让职场办公更为简单!

在当下主打异步沟通的职场办公环境中&#xff0c;我们与很多人的沟通&#xff0c;都是通过书面材料来达成的&#xff0c;这就让 Word 或文档编辑软件变得更为重要&#xff0c;与此同时&#xff0c;有时为了凸现书面材料中的重点&#xff0c;我们还要将 word 文档转换为 ppt 来进…

2024年06月CCF-GESP编程能力等级认证Python编程五级真题解析

本文收录于专栏《Python等级认证CCF-GESP真题解析》&#xff0c;专栏总目录&#xff1a;点这里&#xff0c;订阅后可阅读专栏内所有文章。 一、单选题&#xff08;每题 2 分&#xff0c;共 30 分&#xff09; 第 1 题 在Python中&#xff0c;print((c for c in “GESP”))的输…

MiniGPT-Med 通用医学视觉大模型:生成医学报告 + 视觉问答 + 医学疾病识别

MiniGPT-Med 通用医学视觉大模型&#xff1a;生成医学报告 视觉问答 医学疾病识别 提出背景解法拆解 论文&#xff1a;https://arxiv.org/pdf/2407.04106 代码&#xff1a;https://github.com/Vision-CAIR/MiniGPT-Med 提出背景 近年来&#xff0c;人工智能&#xff08;AI…

如何让自动化测试框架更自动化?

一、引言 ​对于大厂的同学来说&#xff0c;接口自动化是个老生常谈的话题了&#xff0c;毕竟每年的MTSC大会议题都已经能佐证了&#xff0c;不是大数据测试&#xff0c;就是AI测试等等&#xff08;越来越高大上了&#xff09;。不可否认这些专项的方向是质量智能化发展的方向&…

刷题(day02)

1、leetcode136.删除链表的结点 给定单向链表的头指针和一个要删除的节点的值&#xff0c;定义一个函数删除该节点。 返回删除后的链表的头节点。 示例 1: 输入: head [4,5,1,9], val 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点&#xff0c;那么在调用了你的函数…

Windows图形界面(GUI)-SDK-C/C++ - 应用程序结构

公开视频 -> 链接点击跳转公开课程博客首页 -> 链接点击跳转博客主页 目录 入口函数 窗口注册 窗口创建 窗口显示 窗口更新 消息循环 窗口过程 窗口销毁 调试信息 示例代码 入口函数 在Windows应用程序中&#xff0c;WinMain是主函数&#xff0c;作为应用程序…

网格化监控:Eureka与分布式服务网格的协同监控

网格化监控&#xff1a;Eureka与分布式服务网格的协同监控 引言 在微服务架构中&#xff0c;服务网格技术提供了一种有效的方式来管理和监控服务间的通信。Eureka作为Netflix开源的服务发现框架&#xff0c;虽然本身不直接提供服务网格的监控功能&#xff0c;但可以与服务网格…

设计模式探索:适配器模式

1. 适配器模式介绍 1.1 适配器模式介绍 适配器模式&#xff08;adapter pattern&#xff09;的原始定义是&#xff1a;将一个类的接口转换为客户期望的另一个接口&#xff0c;适配器可以让不兼容的两个类一起协同工作。 适配器模式的主要作用是把原本不兼容的接口&#xff0c…

【Python_GUI】thinker布局管理——place方法

place方法可以设置组件的大小以及组件在容器中的精确位置&#xff0c;其参数及含义如下&#xff1a; 参数含义X设置组件距离窗口左侧的水平距离y设置组件距离窗口顶部的垂直距离width设置组件的宽度height设置组件的高度relx设置组件距离窗口左侧的相对距离&#xff0c;范围为…

c++初阶学习----入门(上)

大家好啊。最近学习了一点关于c的知识。这不就迫不及待的来与大家分享了嘛。但我这也是现学现卖所以咧。有很多遗落甚至不对的地方希望大家可以在评论区里面指出来。这样也可以增加大家对知识的巩固。 c语言与c的联系 不知道大家看到c会不会不由自主的联想到C语言啊。毕竟都是…

手机自带录屏在哪?6个软件教你快速进行手机录屏

手机自带录屏在哪&#xff1f;6个软件教你快速进行手机录屏 手机自带的录屏功能可以让你轻松录制屏幕上的内容&#xff0c;记录游戏过程、制作教程或捕捉其他重要时刻。不同品牌的手机可能在不同位置提供录屏功能。以下是一些常见的手机品牌及其录屏功能位置&#xff0c;以及一…

【康复学习--LeetCode每日一题】724. 寻找数组的中心下标

题目&#xff1a; 给你一个整数数组 nums &#xff0c;请计算数组的 中心下标 。 数组 中心下标 是数组的一个下标&#xff0c;其左侧所有元素相加的和等于右侧所有元素相加的和。 如果中心下标位于数组最左端&#xff0c;那么左侧数之和视为 0 &#xff0c;因为在下标的左侧不…

运动爱好者的新选择:哈氪聆光气传导耳机,轻巧又安全

平时不管是漫步街头、骑行穿梭&#xff0c;还是乘坐公共交通时&#xff0c;我总是喜欢佩戴耳机&#xff0c;借此隔绝外部的喧嚣&#xff0c;享受音乐的乐趣。在户外使用耳机&#xff0c;我更倾向于选择气传导耳机&#xff0c;它们更符合我的需求&#xff0c;因为这种耳机能让我…