Oracle數據庫——PL/SQL程序設計綜合案例

一、程序設計方法

1、瀑布模型

在這裏插入圖片描述

2、SQL 語句

3、變量:

  • 初始值是多少
  • 最終值如何得到

二、綜合案例

1、綜合案例1

統計每年入職的員工人數

/*
	SQL語句
		select to_char(hiredate,'yyyy') from emp; 
		-->光標-->循環-->退出條件: not found
	變量: 1.初始值: 2、如何得到:
	每年入職的員工人數:
	count80 number := 0;
	count81 number := 0;
	count82 number := 0;
	count87 number := 0;
*/
set serveroutput on

declare
	--定義光標
	cursor cemp is select to_char(hiredate,'yyy') from emp;
	phiredate varchar2(4) ;
	--定義每年入職的員工人數
	count80 number := 0;
	count81 number := 0;
	count82 number := 0;
	count87 number := 0;
begin
	open cemp;
	loop
		fetch cemp into phiredate; 
		exit when cemp%notfound;
		
		if phiredate = '1980' then count80 := count80+1;
		elsif phiredate = '1981' then count81 := count81+1;
		elsif phiredate = '1982' then count82 := count82+1;
		else count87 := count87+1; 
		end if;
	end loop;
	close cemp;
	dbms_output.put_line( 'total:'||(count80+count81+count82+count87));
	dbms_output.put_line('1980:'||count80) ;
	dbms_output.put_line('1981:'||count81) ;
	dbms_output.put_line('1982: '||count82) ;
	dbms_output.put_line('1987: '||count87) ;
end;
/

在這裏插入圖片描述

2、綜合案例2

爲員工漲工資。從最低工資漲起每人漲10%,但工資總額不能超過5萬元,請計算漲工資的人數和漲工資後的工資總額,並輸出漲工資人數及工資總額。

/*
SQL語句
select empno,sal from emp order by sal;
-->光標-->循環-->退出條件: 1.工資總額> 5W; 2、%notfound

變量: 1、初始值; 2、如何得到
漲工資的人數:
1、select sum(sal) into salTotal from emp;
2、漲後的工資總額=漲前的工資總額+sal*0.1
*/
set serveroutput on
declare
	--定義光標
	cursor cemp is select empno,sal from emp order by sal;
	pempno emp.empno%type;
	psal emp.sal%type;
	--漲工資的人數:
	countEmp number := 0;
	--漲後的工資總額:
	salTotal number;
begin
	--得到工資總額的初始值
	select sum(sal) into salTotal from emp;
	--打開光標
	open cemp;
	
	loop
	--1、工資總額> 5W
	exit when salTotal > 50000;
	--取一個員工漲工資
	fetch cemp into pempno, psal;
	--2、%notfound
	exit when cemp%notfound;
	
	--漲工資
	update emp set sal = sal*1.1 where empno = pempno;
	--人數+1
	countEmp := countEmp + 1;
	--漲後的工資總額=漲前的工資總額+sal*0.1
	salTotal := salTotal + psal*0.1;
	end loop;
	--關閉光標
	close cemp;
	commit;

dbms_output.put_line('人數: '||countEmp||' 漲後的工資總額: '||salTotal);
end;
/

在這裏插入圖片描述

3、綜合案例3

用PL/SQL語言編寫一段程序,實現按部門分段(6000以上、(6000,3000)、3000元以下)統計各工資段的職工人數、以及各部門的工資總額(工資總額中不包括獎金)

create table msg(
	deptno number,
	count1 number,
	count2 number,
	count3 number,
	saltotal number
);
/*
SQL語句
1.有哪些部門
	select deptno from dept-->光標-->循環-->退出條件: not found
2、部門中員工的薪水
	select sal from emp where deptno=? --> 帶一個參數的光標--> 循環--> 退出條件: notfoun
	
count2 number;
count3 number;

每個部門的工資總額:
saltotal number;
1、 select sumsal) into saltotal from emp where deptno=???
*/
set serveroutput on
declare
	--部門的光標
	cursor cdept is select deptno from dept;
	pdeptno dept.deptno%type;
	
	--部門中員工的薪水
	cursor cemp(dno number) is select sal from emp where deptno = dno;
	psal emp.sal%type;
	
	--每個段的員工人數
	count1 number;
	count2 number;
	count3 number;
	
	--每個部門的工資總額
	saltotal number;
begin
	--打開部門的光標
	open cdept;
	loop
		--取出一個部門
    fetch cdept into pdeptno;
		exit when cdept%notfound;

		--初始化的工作
		count1:=0;count2:=0;count3:=0;
		--得到部門的工資總額:
		select sum(sal) into saltotal from emp where deptno=pdeptno;

		--取部門中員工的薪水
		open cemp(pdeptno);
		loop
			--取一個員工的薪水
			fetch cemp into psal;
			exit when cemp%notfound;
		
			--判斷薪水的範圍
			if psal<3000 then count1:=count1+1;
			elsif psal>=3000 and psal<6000 then count2:=count2+1;
			else count3:=count3+1;
			end if;
		end loop;
		close cemp;

		--保存當前部門的結果
		-- nv1();爲慮空函數
		insert into msg values (pdeptno, count1, count2, count3, nvl(SALTOTAL,0));
	end loop;
	
	--關閉部門的光標
	close cdept;
	commit;
	dbms_output.put_line('統計完成');
end;
/

在這裏插入圖片描述
在這裏插入圖片描述

4、綜合案例4

用PL/SQL語言編寫一段程序。按系(系名)分段統計(成績小於60分,60~85分,85分以上)“大學物理”課程各分數段的學生人數,及各系學生的平均成績。

create table msg1 (
	coursename varchar2(20) ,
	dname varchar2(20) ,
	count1 number,
	count2 number ,
	count3 number ,
	avggrade number
);
/*
QL語句
1、得到有哪些系
	select dno, dname from dep;
	-->光標-->循環-->退出條件: not found
2、得到系中,選修了“大學物理”課程學生的成績
	select grade from sc where cno=(select cno from course where cname-'大學物理')
							and sno in (select sno from student shere dno=??);
	-->帶一個參數的光標--> 循環--> 退出條件: notfound

變量: 1.初始值2.如何得到
每個分數段的人數

每個系選修了“大學物理”學生的平均成績:
avggrade number;
1、算術運算
2、select avg(grade) into avggrade from sc where cno=select cno from course whefe cname-'大學物理')
						and sno in (select sno trom student where dno=??);
*/
set serveroutput on
declare
	cursor cdept is select dno,dname from dep;
	pdno dep.dno%type;
	pdname dep.dname%type;
	--成績光標
	cursor cgrade(coursename varchar2, depno number)
		is select grade from sc where cno=(select cno from course where cname=coursename)
							and sno in (select sno from student where dno=depno);
	pgrade sc.grade%type;
	--每個分段的人數
	count1 number; count2 number; count3 number;
	--每個系選修了“大學物理”學生的平均成績
	avggrade number;
	--課程名稱
	pcourseName varchar2(20) := '大學物理';
begin
	--打開系的光標
	open cdept; 
	loop
	--取出一個系的信息
	fetch cdept into pdno, pdname;
	exit when cdept%notfound;
	
	--初始化的工作
	count1:=0; count2:=0; count3:=0;
	--系的平均成績
	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);

	--取系中,選修了大學物理的學生成績
	open cgrade (pcourseName, pdno);
	loop
		--取一個學生的成績
		fetch cgrade into pgrade;
		exit when cgrade%notfound;

	--判斷成績的範圍
	if pgrade<60 then count1 :=count1+1;
	elsif pgrade>=60 and pgrade<85 then count2 :=count2+1;
	else count3:=count3+1;
	end if;
	end loop;
	close cgrade;

	--保存當前的結果
	-- nv1();爲慮空函數
	insert into msg1 values (pcourseName, pdno, count1, count2,count3, avggrade);
end loop;
	
	--關閉系的光標
	close cdept;
	dbms_output.put_line('統計完成');
end;
/

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章