oracle数据库实用教程第二版答案 下载本文

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块,输出所有比部门平均工资高的与员工信息。

declare

cursor c_emp is select * from emp where sal(select avg(sal) from scott.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.sal); end loop;

close c_emp; end; .

(3)编写一个pl/sql块,输出所有员工及部门领导的姓名,员工号及部门号。 declare

cursor c_emp is select e.ename,e.empno,e.deptno,m.ename mname

from emp e inner join emp m on e.empno=m.mgr; v_emp c_emp%rowtype; begin

dbms_output.put_line(empno||||deptno||||ename||||mname); for v_emp in c_emp loop

dbms_output.put_line(v_emp.empno||||v_emp.deptno||||v_emp.ename|| ||v_emp.mname); end loop; end;

(4)查询姓为“smith”的员工信息,并输出其员工号、姓名、工资、部门号。如果该员工不存在,则插入一条新记录,员工号为2012,员工姓名为“smith”,工资为7500,入职日期为“2002年3月5日”,部门号为50.如果存多个名为“smith”的员工,则输出所有名为“smith”的员工号、姓名、工资、入职日期、部门号l。 declare

v_emp emp%rowtype; begin

select * into v_emp from scott.emp where ename=smith; dbms_output.put_line(v_emp.empno||||v_emp.ename|| || v_emp.sal||||v_emp.hiredate||||v_emp.deptno); exception

when no_data_found then

insert into scott.emp(empno,ename,sal,hiredate,deptno) values(2012,smith,7500,5-3月-2002,50); when too_many_rows then

for v_emp in (select * from scott.emp where ename=smith) loop

dbms_output.put_line(v_emp.empno||||v_emp.ename|||| v_emp.sal||||v_emp.hiredate||||v_emp.deptno); end loop; end;

(182)实训题: