当前位置:网站首页>Pl/sql basic case
Pl/sql basic case
2022-07-05 22:02:00 【The sea of waves】
PL/SQL Basic case
Case study 1
problem : 1. Count the number of employees employed each year
Their thinking :
Cycle through the number of employees , Take out the entry date of each employee , Compliant , Counter plus 1
Code :
set severoutput on
declare
-- Define cursor
cursor cemp is select to_char(hiredate,'yyyy') from emp;
phiredate varchar2(4);
-- Number of employees hired each year
count80 number :=0;
count81 number :=0;
count82 number :=0;
count87 numner :=0;
begin
-- Open the cursor
open cemp;
loop
-- Take out the year of employment of an employee
fetch cemp into phiredate;
exit when cemp%notfund;
-- Determine the year of employment
if phiredate = '1980' then count80 :=count80+1;
elsif phiredate ='1981' then count81 :=count81+1;
elseif phiredate ='1982' then count82 :=count82+1;
else count87 := count87+1;
end loop;
-- Close the cursor
close cemp;
end;
/
Case study 2
problem :2 Raise wages for employees . From the minimum wage rise, everyone grows 10%, But the total salary should not exceed 5 Ten thousand yuan , Please calculate the number of people who get a raise and the total salary after the raise , And output the number of wage increases and total wages .
Their thinking :
Code :
set serveroutput on
declare
-- Define cursor
cursor cemp is select empno,sal from emp order by sal;
pempno emp.empno%type;
psal emp.sal%type;
-- The number of people who get a raise
countEmp number := 0;
-- The total salary after the increase
salTotal number;
begin
-- Get the initialization value of the total salary
select sum(sal) into salTotal from emp;
-- Open the cursor
open cemp;
loop
--1, The total salary is greater than 5W
exit when salTotal > 5000;
-- Take an employee for a raise
fetch cemp into pempno ,psal;
exit when cemp%notfund;
-- raise
update emp set sal=sal*1.1 when empno=pempno;
-- The number of +1;
countEmp := countEmp+1;
-- The total salary after the increase = The total amount of wages before the increase + sal * 0.1
salTotal :=salTotal + psal * 0.1;
end loop;
-- Close the cursor
close cemp;
commit;
end;
/
Case study 3
problem
Their thinking
Code
set serveroutput on;
declare
-- Define the cursor
cursor cdept is select dno,dname from dep;
pdno dep.dno%type;
pdname dep.dname%type;
-- Define the cursor of grades
cursor cgrade(coursename varchar2,depno number)
is select grade from sc where cno=(select cno from course where cname = coursename)
and sno = (select sno from student where dno = depno);
pgrade sc.grade%type;
-- The number of people in each segment
count1 number,count2 number,count3 number;
-- Selection of each department “ The average score of college physics students ”
avggrade number;
-- Course name
pcourseName varchar2 := " College Physics ";
begin
-- Open the cursor of the system
open cdept;
loop
-- Take the information of a system
fetch cdept into pdno,pdname;
exit when cdept%notfund;
-- Initialization work
count1 := 0; count2 :=0; count3 :=0;
-- The average grade of the Department
select avg(grade) into avggrade from sc where cno =(select cno from course where cname=pcoursename)
and sno in (select sno from student where dno =pdno )
-- Taking the system , The student's grades of College Physics Department
open cgrade(pcourseName,pdno);
loop
-- Take a student's grade
fetch cgrade into pgrade;
exit when cgrade%notfound;
-- Judge the range of achievement
if pgrade < 60 then count1 := count1+1;
elseif pgrade >=60 and pgrade <=85 then count2 :=count2+1;
else count3 := count3 + 1;
end if;
end loop;
close cgrade;
-- Save the current structure
insert into msg1 values(pcourseName,pdno,count1,count2,count3,avggrade);
end loop;
end;
/
There is something wrong , Welcome to the discussion .
Last , Welcome to pay attention to my wechat , What do you like , Collection , Forwarding is my greatest encouragement .
边栏推荐
- 多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
- 华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
- Matlab | app designer · I used Matlab to make a real-time editor of latex formula
- Blocking of concurrency control
- K210学习笔记(四) K210同时运行多个模型
- Granularity of blocking of concurrency control
- Index optimization of performance tuning methodology
- SecureCRT使用提示
- 总结出现2xx、3xx、4xx、5xx状态码的原因
- Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
猜你喜欢
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
MMAP learning
怎么利用Tensorflow2进行猫狗分类识别
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
The real situation of programmers
Talking about MySQL index
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
Some common processing problems of structural equation model Amos software
QML reported an error expected token ";", expected a qualified name ID
Huawei cloud modelarts text classification - takeout comments
随机推荐
Cross end solutions to improve development efficiency
Experienced inductance manufacturers tell you what makes the inductance noisy. Inductance noise is a common inductance fault. If the used inductance makes noise, you don't have to worry. You just need
How to view Apache log4j 2 remote code execution vulnerability?
K210学习笔记(四) K210同时运行多个模型
Yolov5 training custom data set (pycharm ultra detailed version)
Reptile practice
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
Performance monitoring of database tuning solutions
How to add new fields to mongodb with code (all)
PIP install beatifulsoup4 installation failed
Poj3414广泛搜索
PyGame practical project: write Snake games with 300 lines of code
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
Huawei game multimedia service calls the method of shielding the voice of the specified player, and the error code 3010 is returned
Scenario interview: ten questions and ten answers about distributed locks
About the writing method of SQL field "this includes" and "included in" strings
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
Ad637 notes d'utilisation
Emotional analysis of wechat chat records on Valentine's day based on Text Mining