oracle plsql 练习 要用到游标

本文介绍两个Oracle存储过程的实际应用案例,一是统计不同职位员工的薪资分布情况,二是抓取各职位薪资排名前三的员工信息并存储至指定表中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、编写存储过程,显示各个职位(job),工资在2000元分以上人数和1000元以下人数。
2、编写一个存储过程
将各个职位前3位的职员的姓名,职位,工资,名次按工资排序存到一个数据表GOOD中

解答:
1、
create or replace procedure test_cursor
is
cursor c_tmp1 is select '>=2000' as salway,job,sum(case when sal>=2000 then 1 else 0 end) as countnum from emp group by job ;
cursor c_tmp2 is select '<=1000' as salway,job,sum(case when sal<=1000 then 1 else 0 end) as countnum from emp group by job ;
begin
for r_tmp1 in c_tmp1
loop
dbms_output.put_line('职位'||r_tmp1.job||'大于2000元的人数为'||r_tmp1.countnum);
end loop;
dbms_output.put_line(chr(13)); -- 打印回车
for r_tmp2 in c_tmp2
loop
dbms_output.put_line('职位'||r_tmp2.job||'小于1000元的人数为'||r_tmp2.countnum);
end loop;
end test_cursor;

2、
建表:
drop table good;
create table good(
gid number(5) primary key,
gename varchar2(20) not null,
gejob varchar2(20) not null,
gesal number not null,
grank integer not null
) ;
-- 建序列
create sequence s_good
minvalue 1
maxvalue 99999999999999999999999999
start with 1
increment by 1
cache 20;

-- 建触发器
create trigger t_good before insert on good for each row
begin
select s_good.nextval into :new.gid from dual;
end;

--建过程
create or replace procedure test_cursor2
is
cursor c_tmp is select * from good;
begin
insert into good(gename,gejob,gesal,grank) select * from (select ename gename,job gejob,sal gesal,row_number() over(partition by job order by sal) grank from emp) where grank<4;
commit;
for r_tmp in c_tmp
loop
dbms_output.put_line('姓名:'||r_tmp.gename||',职位:'||r_tmp.gejob||',薪水:'||r_tmp.gesal||',职位排名:'||r_tmp.grank);
end loop;
end test_cursor2;


测试:
begin
--test_cursor;
test_cursor2;
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值