logo资料库

游标实例与返回结果集的存储过程.docx

第1页 / 共2页
第2页 / 共2页
资料共2页,全文预览结束
游标实例: declare cursor emp_cursor(dept_num number:=20) is select ename,sal from emp where deptno=dept_num; v_ename emp.ename%type; v_sal emp.sal%type; --可以使用下面的定义方法,代替上面两个变量 --one_emp emp_cursor%rowtype; begin open emp_cursor(&dnum); loop fetch emp_cursor into v_ename,v_sal; --判断工资低于 2000,增加工资 --if(v_sal<2000) --then update emp set sal=sal*(1.1) where ename=v_ename; --end if; exit when emp_cursor%NOTFOUND; dbms_output.put_line(' 当 前 检 索 的 是 第 '||emp_cursor%rowcount||' 行:'||v_ename||','||v_sal); end loop; close emp_cursor; end; declare --定义游标 sp_emp_cursor type sp_emp_cursor is ref cursor; --定义一个游标变量 test_cursor sp_emp_cursor; --定义变量 v_ename emp.ename%type; v_sal emp.sal%type; begin --执行 --把 test_cursor 和一个 select 结合 open test_cursor for select ename,sal from emp where deptno=&no; --循环取出 loop fetch test_cursor into v_ename,v_sal; --判断是否 test_cursor 为空 exit when test_cursor%notfound; dbms_output.put_line('名字:'||v_ename||' 工资:'||v_sal); end loop;
end; 返回结果集的存储过程实例及调用 number,emp_cursor out or replace PROCEDURE sproc_cursor(deptnum in --创建一个包,定义一个游标类型,为存储过程的输出参数使用 create or replace package sp_emp_pk as type sp_emp_cursor is ref cursor; end sp_emp_pk; / --创建返回结果集的存储过程 create sp_emp_pk.sp_emp_cursor) is begin open emp_cursor for select ename,sal from emp where deptno=deptnum; end sproc_cursor; / --返回结果集的存储过程的调用 declare type sp_emp_cursor is ref cursor; emp_cursor sp_emp_cursor; --v_empno emp.empno%type:=7839; v_deptno emp.deptno%type:=10; v_ename emp.ename%type; v_sal emp.sal%type; begin sproc_cursor(v_deptno,emp_cursor); loop fetch emp_cursor into v_ename,v_sal; exit when emp_cursor%notfound; --sp_pro8(v_empno,v_ename); dbms_output.put_line(v_ename); end loop; --dbms_output.put_line('Hello world'); close emp_cursor; end; /
分享到:
收藏