--3.查询10部门所有员工的姓名。(ref游标实现) 动态游标
declare
--创建一种游标类型
type type_cursor is ref cursor;
--声明变量指定游标类型
v_cur type_cursor;
v_ename emp.ename%type;
begin
open v_cur for select ename from emp where deptno=10;
loop
fetch v_cur into v_ename;
exit when v_cur%notfound;
dbms_output.put_line(v_ename);
end loop;
end;
--5.显示EMP中的第四条记录。 如:游标%rowcount=4
declare
cursor cur_emp is select * from emp;
v_emp emp%rowtype;
begin
open cur_emp;
loop
fetch cur_emp into v_emp;
exit when cur_emp%notfound;
if cur_emp%rowcount=4 then
dbms_output.put_line(‘姓名‘||v_emp.ename);
end if;
end loop;
end;
/
--6.针对所有部门,按以下格式打印各部门人员姓名: 嵌套游标
部门名称:RESEARCH
部门人员:SMITH,JONES,FORD
部门名称:ACCOUNTING
部门人员:CLARK,KING,MILLER,
两种实现提示:
1)循环每个部门,用其部门号作条件去查员工表
2)用显示cursor完成
3)要求用FOR,会用到嵌套循环。
declare --控制部门名称的游标 cursor cur_dept is select dname from dept; --控制员工姓名的游标(作用:显示对应部门下的员工姓名)10,20,30 cursor cur_emp(v_dname dept.dname%type) is select ename from emp where deptno=(select deptno from dept where dname=v_dname); begin --外层循环控制cur_dept 游标(部门名称) for i in cur_dept loop dbms_output.put_line(‘=====部门名称=====‘||i.dname); --内层循环控制cur_emp游标(员工姓名) for j in cur_emp(i.dname) loop dbms_output.put_line(‘员工姓名‘||j.ename); end loop; end loop; end;