当前位置:网站首页>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 .
边栏推荐
- Hysbz 2243 staining (tree chain splitting)
- 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
- Installation of VMware Workstation
- Oracle HugePages没有被使用导致服务器很卡的解决方法
- Blocking protocol for concurrency control
- Poj3414广泛搜索
- Yolov5 training custom data set (pycharm ultra detailed version)
- K210 learning notes (IV) k210 runs multiple models at the same time
- 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
- Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
猜你喜欢
Huawei fast game failed to call the login interface, and returned error code -1
Shell script, awk condition judgment and logic comparison &||
Kingbasees v8r3 cluster maintenance case -- online addition of standby database management node
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
Index optimization of performance tuning methodology
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
华为云ModelArts文本分类–外卖评论
随机推荐
PyGame practical project: write Snake games with 300 lines of code
Emotional analysis of wechat chat records on Valentine's day based on Text Mining
微服務鏈路風險分析
Two stage locking protocol for concurrency control
Database recovery strategy
CRM creates its own custom report based on fetch
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
如何开发引入小程序插件
每日刷题记录 (十四)
初级软件测试必问面试题
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
Basic grammar of interview (Part 1)
DataGrid directly edits and saves "design defects"
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
2.2.5 basic sentences of R language drawing
Analyse des risques liés aux liaisons de microservices
Storage optimization of performance tuning methodology
Daily question brushing record (XIV)
Summary of El and JSTL precautions