当前位置: 首页 > news >正文

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;
 

相关文章:

PLSQL Day4

--使用显式游标更新行&#xff0c;对所有salesman增加500奖金&#xff1a; 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_cur…...

git合并报错:git -c core.quotepath=false -c log.showSignature=false merge r

这个错误通常发生在 Git 尝试合并两个没有共同祖先的历史时&#xff0c;比如在合并不同的分支或仓库时&#xff0c;可以尝试以下几种方法&#xff1a; 允许不相关历史的合并: git merge release-3.6 --allow-unrelated-histories这个选项告诉 Git 允许合并两个没有共同历史的分…...

云原生存储:使用MinIO与Spring整合

在现代云原生应用开发中&#xff0c;高效、可靠的存储解决方案是至关重要的。MinIO是一个高性能、分布式的对象存储系统&#xff0c;它与Amazon S3兼容&#xff0c;非常适合在Kubernetes等云原生环境中使用。本文将详细介绍如何在Spring Boot应用中整合MinIO&#xff0c;并提供…...

等保测评新趋势:应对数字化转型中的安全挑战

随着信息技术的飞速发展&#xff0c;数字化转型已成为企业提升竞争力、优化运营效率的重要手段。然而&#xff0c;这一转型过程中&#xff0c;企业也面临着前所未有的安全挑战。等保测评&#xff08;信息安全等级保护测评&#xff09;作为保障信息系统安全的重要手段&#xff0…...

使用esptool工具备份ESP32的固件(从芯片中备份下来固件)

本文以Windows电脑为例 板子为esp32-c3 1下载python 可在官网中下载,此处不进行讲解 使用如下代码查看是否安装了 Python&#xff08;终端输入&#xff09; python 2下载esptool 在终端输入如下代码即可下载 使用 pip&#xff08;推荐&#xff09;: 在你已经安装的 Pyth…...

JS进阶-解析赋值

学习目标&#xff1a; 掌握解析赋值 学习内容&#xff1a; 解构赋值数组解构对象解构筛选数组filter方法&#xff08;重点&#xff09; 解构赋值&#xff1a; 解构赋值是一种快速为变量赋值的简洁语法&#xff0c;本质上仍然是为变量赋值。 分为&#xff1a; 数组解构对象解…...

Java虚拟机面试题汇总

目录 1. JVM的主要组成部分及其作用&#xff1f; 1.1 运行时数据区划分&#xff1f; 1.2 哪些区域可能会发生OOM&#xff1f; 1.3 堆和栈的区别&#xff1f; 1.4 内存模型中的happen-before是什么&#xff1f; 2. HotSpot虚拟机对象创建流程&#xff1f; 2.1 类加载过程…...

C++休眠的方法

Windows的API函数 Sleep(INFINITE); 休眠时间为永久 Linux的API函数sleep 没有直接表示无限时间的参数&#xff0c;根据POSIX标准&#xff0c;sleep() 函数的参数应该是 unsigned int 类型&#xff0c;因此最大可以接受的参数值是 UINT_MAX&#xff0c;即 4294967295 秒。sleep…...

选择排序(C语言版)

选择排序是一种简单直观的排序算法 算法实现 首先在未排序序列中找到最小&#xff08;大&#xff09;元素&#xff0c;存放到排序序列的起始位置。 再从剩余未排序元素中继续寻找最小&#xff08;大&#xff09;元素&#xff0c;然后放到已排序序列的末尾。 重复第二步&…...

基于CentOS Stream 9平台搭建FRP内网穿透

内网穿透方法很多&#xff0c;本文以github上很火的frp为例 1.frp官方 文档&#xff1a;https://gofrp.org/zh-cn/docs/overview/ 1.1 下载 https://github.com/fatedier/frp/releases 选中合适的版本 2. 服务端&#xff08;服务器&#xff09;搭建frps 需要公网IP服务器 选…...

Redis管理禁用命令

在redis数据量比较大时&#xff0c;执行 keys * &#xff0c;fluashdb 这些命令&#xff0c;会导致redis长时间阻塞&#xff0c;大量请求被阻塞&#xff0c;cpu飙升&#xff0c;严重可能导致redis宕机&#xff0c;数据库雪崩。所以一些命令在生产环境禁止使用。 Redis 禁用命令…...

RFID智能锁控系统在物流安全运输中的应用与效益分析

一、物流锁控系统现状与挑战 1.1 传统锁控系统的局限性 安全性不足&#xff1a;机械锁容易被撬开或钥匙被复制&#xff0c;导致货物在运输过程中面临被盗风险。 无法实时追踪&#xff1a;一旦货物离开发货点&#xff0c;物流公司无法实时监控货物状态&#xff0c;增加了货物…...

WPF设置全局样式

目的 创建一个资源字典&#xff0c;自动引入到各个Window或者UserControl中&#xff0c;可以随意使用。或者引入多个控件包&#xff0c;为了做兼容&#xff0c;保证可以引用多个控件库。 1. 定义资源字典 首先&#xff0c;你需要创建一个XAML文件来定义你的资源字典&#xf…...

【福利】代码公开!咸鱼之王自动答题脚本

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;欢迎[点赞、收藏、关注]哦~ 微信或QQ打开咸鱼之王小程序&#xff0c;进入答题界面&#xff0c;运行main.py。期间不要动鼠标。 可自行更改代码来适配自己的需求~ 可以按照示例图片…...

ChatGPT-4o大语言模型优化、本地私有化部署、从0-1搭建、智能体构建技术

在过去几年中&#xff0c;人工智能领域的发展迅猛&#xff0c;尤其是大语言模型的应用&#xff0c;为各行各业带来了前所未有的创新与突破。从ChatGPT-3.5的推出到GPT Store的上线&#xff0c;再到最新的多模态交互ChatGPT-4o&#xff0c;OpenAI不断引领科技潮流&#xff0c;推…...

使用clion刷leetcode

如何优雅的使用clion刷leetcode 安装插件&#xff1a;LeetCode Editor) 插件配置&#xff1a; 这样我们每打开一个项目&#xff0c;就会创建类似的文件 我们的项目结构&#xff1a; 我们在题解文件中导入头文件myHeader.h并将新建的文件添加到cmakelists.txt文件&#xff0c;…...

图解HTTP(5、与 HTTP 协作的 Web 服务器 6、HTTP 首部)

5、与 HTTP 协作的 Web 服务器 一台 Web 服务器可搭建多个独立域名的 Web 网站&#xff0c;也可作为通信路径上的中转服务器提升传输效率。 用单台虚拟主机实现多个域名 在相同的 IP 地址下&#xff0c;由于虚拟主机可以寄存多个不同主机名和域名的 Web 网站&#xff0c;因此…...

JS之防抖和节流

防抖 (debounce) 所谓防抖&#xff0c;就是指触发事件后在 n 秒内函数只能执行一次&#xff0c;如果在 n 秒内又触发了事件&#xff0c;则会重新计算函数执行时间。 ps: 重置普攻&#xff0c;百度翻译要输完停止一定时间后才翻译。 没有防抖和节流的缺点&#xff1a; 函数触发…...

Open3D 点云PCA算法配准(粗配准)

目录 一、概述 1.1PCA配准的原理 1.2PCA配准的应用 二、代码实现 三、实现效果 3.1原始点云 3.2配准后点云 3.3变换矩阵 一、概述 PCA(Principal Component Analysis,主成分分析)是一种用于降维和特征提取的统计方法。在点云处理中,PCA可以用于点云配准(a…...

Transformer中的编码器和解码器结构有什么不同?

Transformer背后的核心概念&#xff1a;注意力机制&#xff1b;编码器-解码器结构&#xff1b;多头注意力等&#xff1b; 例如&#xff1a;The cat sat on the mat&#xff1b; 1、嵌入&#xff1a; 首先&#xff0c;模型将输入序列中的每个单词嵌入到一个高维向量中表示&…...

LM265 手持式频谱分析仪:交通超宽频监测旗舰

LM265 手持式频谱分析仪是成都鼎讯信通科技打造的超宽频高性能便携设备&#xff0c;覆盖 9kHz~26.5GHz&#xff0c;射频指标对标台式仪器&#xff0c;兼顾便携与精度&#xff0c;为铁路、高速等交通领域提供全频段信号监测与干扰排查能力。设备集成频谱分析、场强测量、信道扫描…...

数学科研效率提升300%,NotebookLM辅助建模全流程解析,含独家提示词矩阵与误差校验协议

更多请点击&#xff1a; https://intelliparadigm.com 第一章&#xff1a;NotebookLM数学研究辅助的范式革命 传统数学研究长期依赖纸笔推演、孤立文献查阅与手工公式验证&#xff0c;而NotebookLM通过其独特的“语义锚点双文档协同推理”机制&#xff0c;重构了从问题建模到定…...

c语言csv文件?_?C语言中读取和写入csv文件的标准文件操作函数实现.txt

用map实现slice去重最常用也最稳妥&#xff0c;核心是将元素作为key存入map[interface{}]struct{}&#xff0c;再遍历构建新slice&#xff1b;注意元素需可比较&#xff0c;结构体不可含slice/map/func&#xff0c;该方法保持顺序但不并发安全。用 map 实现 slice 去重最常用也…...

【Qt串口实战】硬件升级后readyRead信号丢失的排查与修复

1. 问题现象&#xff1a;硬件升级后readyRead信号神秘消失 那天早上刚到公司&#xff0c;硬件组的同事兴冲冲地跑过来告诉我&#xff1a;"老王&#xff0c;我们给设备升级了最新固件&#xff0c;性能提升30%&#xff01;"我心想这是好事啊&#xff0c;结果打开调试软…...

3分钟解决Windows软件兼容性难题:Visual C++运行库一键修复全攻略

3分钟解决Windows软件兼容性难题&#xff1a;Visual C运行库一键修复全攻略 【免费下载链接】vcredist AIO Repack for latest Microsoft Visual C Redistributable Runtimes 项目地址: https://gitcode.com/gh_mirrors/vc/vcredist 你是否曾因游戏无法启动而沮丧&#…...

C#上位机与三菱PLC通信实战:从零构建GX Works3仿真平台

1. 为什么需要搭建GX Works3仿真平台 第一次接触三菱PLC开发的朋友们&#xff0c;可能都有这样的困惑&#xff1a;手头没有实体PLC设备&#xff0c;怎么测试自己写的控制程序&#xff1f;买一台FX5U PLC动辄几千元&#xff0c;对个人开发者来说成本太高。这时候仿真平台就成了最…...

RT-Thread Nano串口控制台移植:GD32F470实现rt_kprintf打印与调试

1. 项目概述与核心目标上次我们成功把 RT-Thread Nano 内核搬到了梁山派 GD32F470 开发板上&#xff0c;让这块高性能的 MCU 跑起了实时任务调度。但光有内核&#xff0c;就像给电脑装好了操作系统却没法敲命令、看输出&#xff0c;调试和交互起来非常别扭。官方 Nano 包的精髓…...

稀疏矩阵运算全解析:从基础算术到高效求解与性能调优

1. 稀疏矩阵运算操作全景解析在数值计算、机器学习、图形学乃至各类工程仿真领域&#xff0c;处理大规模数据时&#xff0c;我们总会遇到一个“熟悉的陌生人”——稀疏矩阵。它不像密集矩阵那样&#xff0c;每个元素都占据着内存空间&#xff0c;而是像一个精打细算的管家&…...

Context-Mode:基于React Context的模式化状态管理新范式

1. 项目概述&#xff1a;一个为现代前端开发量身定制的状态管理新范式 最近在重构一个中后台项目时&#xff0c;我又一次陷入了状态管理的泥潭。组件间层层传递的 props 像一团乱麻&#xff0c;全局 store 里塞满了各种不相关的数据&#xff0c;每次修改一个状态都得小心翼…...

国产碳化硅MOSFET在通讯电源PFC中的应用与实战解析

1. 项目概述&#xff1a;当通讯电源遇上国产碳化硅MOSFET最近在做一个通讯电源的PFC&#xff08;功率因数校正&#xff09;项目&#xff0c;客户对效率、功率密度和可靠性提出了近乎苛刻的要求。传统的硅基MOSFET方案&#xff0c;在追求更高开关频率以减小磁性元件体积时&#…...