Oracle初探(六)

今天进行了游标的学习,学习笔记如下:
--游标
--定义游标
--打开游标
--读取数据
--关闭游标
declare
  cursor v_cursor is select ename,job from emp;
  v_name varchar2(20);
  v_job varchar2(20);
begin
  open v_cursor;
  fetch v_cursor into v_name,v_job;
  dbms_output.put_line(v_name||'    '||v_job);
  close v_cursor; 
end;
/

-------------输出emp所有数据
declare
  cursor v_cursor is select ename,job from emp;
  v_name emp.ename%type;
  v_job emp.job%type;
  v_id emp.empno%type;
begin
  open v_cursor;
  for v_id in 1..14 loop
      fetch v_cursor into v_name,v_job;
      dbms_output.put_line(v_name||'    '||v_job);
  end loop;
  close v_cursor; 
end;
/
----------------定义游标变量读取数据
declare
  cursor v_cursor is select e.ename,e.job,d.dname from emp e,dept d where e.deptno=d.deptno;
  cursor_record v_cursor%rowtype;
begin
  open v_cursor;
  loop
      fetch v_cursor into cursor_record ;
      exit when v_cursor%notfound;
      dbms_output.put_line(cursor_record.ename||'    '||cursor_record.job||'    '||cursor_record.dname);
  end loop;
  close v_cursor; 
end;
/
---------------------带参游标
declare
  cursor v_cursor(v_deptno number) is select e.ename,e.job,d.dname from emp e,dept d where e.deptno=d.deptno and e.deptno=v_deptno;
  cursor_record v_cursor%rowtype;
begin
  open v_cursor(&no);
  loop
      fetch v_cursor into cursor_record ;
      exit when v_cursor%notfound;
      dbms_output.put_line(cursor_record.ename||'    '||cursor_record.job||'    '||cursor_record.dname);
  end loop;
  close v_cursor; 
end;
/
--练习
---------------------------显示各个部门员工的姓名、工资及部门总工资
declare
    cursor cursor_dept is select deptno,loc from dept;
    cursor cursor_emp(v_deptno number) is select ename,sal from emp where deptno = v_deptno;
    
    v_deptno number(8);
    v_loc varchar2(20);
    
    v_ename varchar2(20);
    v_sal number(8);
    
    v_sum number(8) :=0;
    
begin
  
    open cursor_dept;

    loop
          fetch cursor_dept into v_deptno,v_loc;
          exit when cursor_dept%notfound;
          dbms_output.put_line('部门编号:' || v_deptno || ' ====================== ' || '部门地址:' || v_loc);
          
          open cursor_emp(v_deptno);
          
          loop
            fetch cursor_emp into v_ename,v_sal;
            exit when cursor_emp%notfound;
             dbms_output.put_line('姓名:' || v_ename || '工资:' || v_sal);
             
             v_sum := v_sum + v_sal;
          end loop;
          close cursor_emp;
          dbms_output.put_line(v_deptno || '号部门的总工资:' || v_sum);
          
          v_sum :=0;
          dbms_output.put_line(' ');
    end loop;
    close cursor_dept;
end;
/
-------------------ref游标
declare
    type ref_cursor is ref cursor;
    v_cursor ref_cursor;
    v_emp emp%rowtype;
begin
    open v_cursor for select * from emp;
    loop
         fetch v_cursor into v_emp;
         exit when v_cursor%notfound;
         dbms_output.put_line('编号:'||v_emp.ename||'工资:'||v_emp.sal);
    end loop;
end;
/
-------------------隐式游标
declare
    cursor v_cursor is select * from emp;
begin
    for i in v_cursor loop
      dbms_output.put_line('编号:'||i.ename||'工资:'||i.sal);
    end loop;
end;
/
---------------------练习
declare
    cursor cursor_dept is select deptno,loc from dept;
    cursor cursor_emp(v_deptno number) is select ename,sal from emp where deptno = v_deptno;
    v_deptno number(8);
    v_sum number(8) :=0;
begin
    for i in cursor_dept loop
          exit when cursor_dept%notfound;
          dbms_output.put_line('部门编号:' ||i.deptno || '============ ' || '部门地址:' || i.loc);
          
          for j in cursor_emp(i.deptno) loop
            exit when cursor_emp%notfound;
             dbms_output.put_line('姓名:' || j.ename || '工资:' || j.sal);
             
             v_sum := v_sum + j.sal;
          end loop;
          dbms_output.put_line(v_deptno || '号部门的总工资:' || v_sum);
          
          v_sum :=0;
          dbms_output.put_line(' ');
    end loop;
end;
/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

馥影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值