PL/SQL块练习
2017-11-06 本文已影响0人
孤意的学习笔记
1、自动以输入任意员工编号,输出该员工编号,输出该员工变号、姓名、工资、部门、所在地
declare
-- Local variables here
empno integer;
ename nvarchar2(20);
sal float;
dname nvarchar2(20);
loc nvarchar2(20);
i integer;
begin
-- Test statements here
select empno,ename,sal,dname,loc into empno,ename,sal,dname,loc from emp join dept on emp.deptno= dept.deptno where empno = &i;
dbms_output.put_line(empno||' '||ename||' '||sal||' '||dname||' '||loc);
end;
2、自定义输入任意员工编号;如果该员工入职时间大于10年,则奖金加1w,如果该员工入职时间大于5年,奖金加5000,否则不加。最终输出员工编号、姓名、入职时间、原奖金、现奖金
declare
-- Local variables here
empno integer;
ename nvarchar2(20);
comm_before integer;
comm_after integer;
hiredate date;
i integer;
t integer;
begin
-- Test statements here
select empno,ename,hiredate,comm into empno,ename,hiredate,comm_before from emp join dept on emp.deptno= dept.deptno where empno = &i;
select months_between(sysdate,hiredate) into t from emp where empno = &i;
if t>=120 then
update emp set comm=comm+10000 where empno=i;
elsif t>=60 and t<120 then
update emp set comm=comm+5000 where empno=i;
elsif t<60 then
update emp set comm=comm where empno=i;
end if;
select comm into comm_after from emp where empno=i;
dbms_output.put_line(empno||' '||ename||' '||hiredate||' '||comm_before||' '||comm_after);
end;
/
3、对每位员工的薪水进行判断,如果该员工薪水高于其所在部门薪水,则将其薪水减50元,否则不变
declare
-- Local variables here
myempno emp.empno%type := '&no';
empeg scott.emp%rowtype;
avgSal number;
saleg number;
depteg scott.dept%rowtype;
begin
-- Test statements here
select * into empeg from scott.emp where emp.empno = myempno;
select * into depteg from scott.dept where dept.deptno=empeg.deptno;
select avg(sal) into avgSal from scott.emp group by emp.deptno having emp.deptno=empeg.deptno;
select emp.sal into saleg from emp where emp.empno = myempno;
if empeg.sal>avgSal then
saleg := saleg-50;
end if;
dbms_output.put_line('员工编号:' || myempno || '姓名:' || empeg.ename || '之前工资:' || empeg.sal || '现在工资:' || saleg);
end;
/
4、创建一个存储过程,实现:通过输入员工编号查看员工姓名、工资、奖金;
1.1 如果输入的编号不存在,则进行异常处理;
1.2 如果工资高于4000,输出工资高于4000;
1.3 如果奖金没有或为0,进行异常提示处理
create or replace procedure procemp(myempno in int) is
-- Local variables here
empeg scott.emp%rowtype;
ifExist number;
begin
-- Test statements here
select * into empeg from scott.emp where emp.empno = myempno;
select count(1) into ifExist from scott.emp where emp.empno = myempno;
if ifExist=0 then
dbms_output.put_line('错误!编号不存在');
elsif empeg.sal>4000 then
dbms_output.put_line('工资高于4000');
elsif nvl(empeg.comm,0)=0 then
dbms_output.put_line('奖金为0');
else null;
end if;
dbms_output.put_line('员工编号:' || myempno || ' 姓名:' || empeg.ename || ' 工资:' || empeg.sal || ' 奖金:' || empeg.comm);
end procemp;