日期:2014-05-17 浏览次数:21194 次
create or replace procedure fenye(tablename in varchar2, pagesize1 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) * pagesize1 + 1; v_end number := pagenow * pagesize1; begin --执行部分 v_sql := 'select ename from (select t1.*, rownum rn from (select * from ' || tablename || ' order by sal) t1 where rownum<=' || v_end || ') where rn >=' || v_begin; --把游标和SQL关联 open p_cursor for v_sql; --计算 myrows 和 mypagecount --@组织一个sql语句 v_sql := 'select count(*) from ' || tablename; --执行sql,并把返回值赋给 myrows execute immediate v_sql into myrows; --计算mypagecount if mod(myrows, pagesize1) = 0 then mypagecount := myrows / pagesize1; else mypagecount := myrows / pagesize1 + 1; end if; end;
------解决方案--------------------
SQL> declare 2 v_count number; 3 v_pagecount number; 4 v_cur_emp testpackage.test_cursor; 5 v_emp emp.ename%type; 6 begin 7 fenye('emp', 3, 2, v_count, v_pagecount, v_cur_emp); 8 loop 9 fetch v_cur_emp into v_emp; 10 exit when v_cur_emp%notfound; 11 dbms_output.put_line(v_emp); 12 end loop; 13 end; 14 / WARD MART IN MILL ER PL/SQL procedure successfully completed SQL>