加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

2018.6.5 Oracle plsql编程 游标的使用

发布时间:2020-12-12 13:34:53 所属栏目:百科 来源:网络整理
导读:--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; l

--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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读