您的位置:首页 > 数据库

PLSQL练习

2016-05-15 21:38 316 查看
--使用pl/sql块编程实现,注意必要的异常处理。

--1 输入一个员工号,输出该员工的姓名、薪金和工作时间(按年月日显示)。

DECLARE

emp_name SCOTT.EMP.ename%type;

emp_sal SCOTT.EMP.sal%type;

emp_hiredate SCOTT.EMP.hiredate%type;

BEGIN

  select ename,sal,hiredate into emp_name,emp_sal,emp_hiredate from emp where empno=&empno;

  dbms_output.put_line('姓名:'||emp_name||' 工资:'||emp_sal||' 入职时间:'||to_char(emp_hiredate,'yyyy-mm-dd'));

exception when others THEN

  dbms_output.put_line('发生异常!');

end;

--2 接收一个员工号,输出该员工所在部门的名称。

DECLARE

dept_dname dept.dname%type;

BEGIN

  select dname into dept_dname from emp e,dept d where e.deptno=d.deptno and empno=&empno;

  dbms_output.put_line('部门名称:'||dept_dname);

exception when others THEN

  dbms_output.put_line('发生异常!');

end;

--3 接收一个员工号,如果该员工职位是MANAGER,并且在DALLAS工作,那么就给他薪金加15%;如果该员工职位是CLERK,并且在NEW YORK工作,那么就给他薪金扣除5%;其他情况不作处理。

declare

    emp_job emp.job%type;

    dept_loc dept.loc%type;

    emp_sal emp.sal%type;

    emp_no emp.empno%type;

begin

  select job,loc,sal,e.empno into emp_job,dept_loc,emp_sal,emp_no from emp e,dept d where d.deptno=e.deptno and empno=&empno;

  if emp_job='MANAGER' and dept_loc='DALLAS' then

    emp_sal:=emp_sal*1.15; 

  elsif emp_job='CLERK' and dept_loc='NEW YORK' then

      emp_sal:=emp_sal*0.95;

  end if;

  dbms_output.put_line('职位:'||emp_job||' 工作地点:'||dept_loc||' 工资:'||emp_sal);

 update emp set sal=emp_sal where empno=emp_no;

exception when others THEN

  dbms_output.put_line('发生异常!');

end;

    

--4 接收一个员工号,输出这个员工所在部门的平均工资。

DECLARE

dept_avgsal emp.sal%type;

BEGIN

  select avg(sal) into dept_avgsal from emp e,dept d where e.deptno=d.deptno and e.deptno=(select deptno from emp where empno=&empno) group by e.deptno;

  dbms_output.put_line('该部门平均工资:'||dept_avgsal);

exception when others THEN

  dbms_output.put_line('发生异常!');

end;

--5 要求输入一个雇员编号,为此雇员增长工资,增长工作按照以下的原则进行:  · 10部门人员工资上涨10% · 20部门人员工资上涨20% · 30部门人员工资上涨30% 但是所有的工资最高不超过5000。

DECLARE

emp_sal EMP.sal%type;

dept_no EMP.deptno%type;

emp_no emp.empno%type;

BEGIN

select empno,deptno,sal into emp_no,dept_no,emp_sal from emp where empno=&empno;

if dept_no=10 THEN

emp_sal:=emp_sal*1.1;

elsif dept_no=20 THEN

emp_sal:=emp_sal*1.2;

elsif dept_no=30 THEN

emp_sal:=emp_sal*1.3;

elsif emp_sal>5000 THEN

emp_sal:=5000;

end if;

dbms_output.put_line('部门编号:'||dept_no||' 工资:'||emp_sal);

update emp set sal=emp_sal where empno=emp_no;

exception when others THEN
dbms_output.put_line('发生异常!');

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: