Oracle 11g数据库基础教程(第2版)-课后习题-第九章

----第九章
--1.编写一个PL/SQL块,输出所有员工的员工姓名、员工号、工资和部门号
set serveroutput on
declare
	cursor c_emp is select * from emp;
	v_emp c_emp%rowtype;
begin
	open c_emp;
	loop
		fetch c_emp into v_emp;
		exit when c_emp%notfound;
		dbms_output.put_line(v_emp.ename || ' ' || v_emp.empno || ' ' || v_emp.sal || ' ' || v_emp.deptno);
	end loop;
	close c_emp;
	end;
/

--2.编写一个PL/SQL块,输出所有比本部门平均工资高的员工信息
set serveroutput on 
declare
	cursor c_emp is select * from emp where sal>(select avg(sal) from emp);
	v_emp c_emp%rowtype;
begin
	open c_emp;
	fetch c_emp into v_emp;
	while c_emp%found loop
		dbms_output.put_line(v.emp.ename || ' ' || v_emp.empno || ' ' || v_emp.sal || ' ' v_emp.deptno);
		fetch c_emp into v_emp;
	end loop;
	close c_emp;
end;
/

--3.编写一个PL/SQL,输出所有员工及其部门领导的姓名,员工号以及部门号
set serveroutput on 
declare
	cursor c_emp is 
	select e.empno eid,e.ename ename,e.deptno edid,m.empno mid,m.ename mname from emp e join emp m on e.mgr=m.empno;
	v_emp c_emp%rowtype;
	begin
	open c_emp;
	loop 
		fetch c_emp into v_emp;
		exit when c_emp%notfound;
		dbms_output.put_line(v_emp.eid || ' ' ||v_emp.ename || ' ' || v_emp.edid || ' ' || v_emp.mid || ' ' || v_emp.mname);
	end loop;
	close c_emp;
end;
/
--4.查询性“Smith"的员工信息,并输出起员工号、性别、工资、部门号。如果该员工不存在,则插入一个条新记录,员工号为2010,员工性为“Smith"
--工资编号AD_VP,部门号为50.如果存在多个名为“Smith”的员工,则输出所有名为“Smith"的员工号,姓名,工资,入职日期,部门号,email
set serveroutput on 
declare
	v_emp emp%rowtype;
begin
	select * into v_emp from emp where ename='Smith';
	dbms_output.put_line(v_emp.empno || ' ' || ' ' || v_emp.ename || ' ' ||v_emp.sal || ' ' ||v_emp.deptno);
exception
	when no_data_found then
	insert into dept values(50,'aa','CHINA');
	insert into emp(empno,ename,sal,hiredate,job,deptno)values(2010,'Smith',7500,to_date('2010-10-5','yyyy-mm-dd'),'AD_VP',50);
	WHEN TOO_MANY_ROWS THEN 
		FOR v_emp in (select * from emp where ename = 'SCOTT') loop
		dbms_output.put_line(V_EMP.EMPNO || ' ' || V_EMP.ENAME || ' '|| V_EMP.SAL || ' ' || V_EMP.HIREDATE || ' ' || V_EMP.DEPTNO);
	END LOOP;
END;
/
--5.编写一个PL/SQL块,根据员工职位不同更新员工的工资,职位为AD_PRES,AD_VP,AD_ASST的员工工资增加1000元,职位为FI_MGR.FI_ACCOUNT的员工工资增加800元
--职位为AC_MGR,AC_ACCOUNT的员工工资增加700元,职位为sa_man,sa_rep的员工工资增加600元,职位为PU_MAN,pu_clerk的员工工资增加500元,职位为st_man,st_cterk,sh_clerk的员工工资增加400元
--职位为it_prog,mk_man,mk_rep的员工工资增加300元,其他职位的员工工资增加200元
declare
	CURSOR C_EMP IS SELECT * FROM EMP;
	V_INC EMP.SAL%TYPE;
begin
	FOR V_EMP IN c_emp loop
		case 
			when v_emp.job = 'AD_PRES' OR V_EMP.job = 'ad_vp' or v_emp.job='ad_asst' then
			v_inc := 1000;
			when v_emp.job='FI_MGR' or v_emp.job='FI_ACCOUNT' 
			then v_inc:=700;
			when v_emp.job='SA_MAN' or v_emp.job='SA_REP' 
			then v_inc:=600;
			when v_emp.job='PU_MAN' or v_emp.job='PU_CLERK'
			then v_inc:=500;
			when v_emp.job='ST_MAN' or v_emp.job='ST_CLERK' or v_emp.job='SH_CLERK'
			then v_inc:=400;
			when v_emp.job='IT_PROG' or v_emp.job='MK_MAN' or v_emp.job='MK_REP'
			then v_inc:=300;
	 	else v_inc:=200;
		 	end case;
		 	update emp set sal = sal+v_inc where empno = v_emp.empno;
	end loop;
end;
/
--6编写一个PL/SQL快,修改员工号为201的员工工资为8000元,保证修改后的工资在职位的工资范围之内,否则取消操作,并说明原因
declare
	v_lo salgrade.losal%type;
	v_hi salgrade.hisal%type;
	e exception;
begin
	update emp set sal = 8000 where empno =2010;
	select losal,hisal into v_lo,v_hi from salgrade;
	if 8000 not between v_lo and v_hi then
	raise e;
	end if;
	exception
		when e then
			raise_application_error(-20001,'beyond limit');
		rollback;
	end;
/

你可能感兴趣的:(Oracle,11g)