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;
--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;
相关文章推荐
- Oracle闪回开启及操作
- Memcache和Redis对比
- Oracle 中的 TO_DATE 和 TO_CHAR 函数 日期处理
- MySQL常用函数示例
- 数据库性能优化
- Oracle 身份验证方式
- Oracle 删除用户和表空间
- SQL的四种连接-左外连接、右外连接、内连接、全连接
- Oracle synonyms
- Oracle同义词(synonyms)的概念作用、创建删除查看及Oracle的db link
- Oracle expdp/impdp导出导入命令及数据库备份
- mysql sql
- 数据库索引的优缺点
- jdbc在mysql下一次执行多条sql脚本
- 360开源的类Redis存储系统:Pika
- MySQL资源大全
- MySQL常用函数
- 在pandas中使用sql
- Redis分布式解决方案-Coids
- PL/SQL Developer 的安装与配置