有返回值的存储过程(列表 结果集)

 案例:编写一个过程,输入部门编号,返回该部门所有员工的信息。对该题的分析如下:

由于Oracle的存储过程没有返回值,它的所有返回值都是通过out参数来代替的,列表同样也不例外,但是由于是集合,所以不能用一般的参数,必须要用package,所以分两部分:

Oralce 写一个分页的存储过程_ocp

1)建一个包。如下:


create or replace package testpackage AS TYPE test_cursor


is ref cursor;

end testpackage;


在该包中我定义了一个游标类型  test_cursor


下面就是写创建过程了

create or replace procedure chenchuang_pro

(chenNo in number p_cursor out testpackage.test_cursor) is

begin

open p_cursor for select * from emp wheredeptno-chenNo;

end;



---------------------------------------------------------

要求,编写一个存储过程,要求可以输入表名,每页显示记录数,当前页,返回总记录数,总页数,和返回结果集。(也就是写一个


分页的存储过程)


Oracle 的分页:

在分页的时候,把下面的sql语句当作模板使用


select * from (select t1.* , rownum rn from (select * from


emp)t1 where rn<=10) where rn>=6



--开始编写分页的过程



先写一个包

create or replace package testpackage AS TYPE test_cursor


is ref cursor;

end testpackage;


 


created or replace procedure fenye

(tableName in varchar2,

  PageSize in number;--每页显示的数据量

  PageNow in number;--当前的页码

  myRows out number;--总记录数

  myPageCount out number;--中页数

  P_cursor out testpackage.test_cursor-- 返回的记录集

) is

-- 定义部分

---定义sql语句

  v_sql varchar2(1000);

  v_begin number:=(PageNow-1)*PageSize+1;--分页算法

  v_end number :=PageNow*PageSize;

begin

--执行部分

  v_sql:='select * from (select t1.* , rownum rn from (select * from '|| tableName||')t1 where rn<='||v_end||')  where rn>='||v_begin;

--把游标和sql关联

open p_cursor for v_sql;

--计算MyRows 和MyJpageCount

--组织一个sql

v_sql:='select count(*) from '|| tableName

--执行sql,并把返回值 赋给MyRows

excute immediate v_sql into MyRows;

--计算myPageCount

if mod (myRows,PageSize )=0 then

  myPageCount:=myRows/PageSize;

 else myPageCount:=myRows/PageSize+1;

end if ;

--关闭游标

close p_cursor;


end