zoukankan      html  css  js  c++  java
  • Oracle笔记:分页查询

    查询前三条记录:

    select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum<=3);

    查询第三到六条记录:

    select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum<=6) where rn>3;

    如要指定查询列,排序等,只需修改最里层的子查询.

    编写分页存储过程

    --编写一个存储过程,要求可以输入表名、每页显示记录数、当前页、
    --返回总记录数、总页数和返回的结果集。
    create or replace package test_package as
    type test_cursor is ref cursor;
    end test_package;

    create or replace procedure paging
    (tableName in varchar2,   --表名
    recordPerPage in number,  --每页显示记录数
    currentPage in number,    --当前页
    totalRecord out number,   --总记录数
    totalPage out number,    --总页数
    t_cursor out test_package.test_cursor  --返回的结果集
    ) is  
    v_sql varchar2(1000);
    v_start number:=(currentPage-1)*recordPerPage+1;
    v_end number:=currentPage*recordPerPage;
    begin
    v_sql:='select * from (select a1.*,rownum rn from (select * from '||tableName
    ||') a1 where rownum<='||v_end||') where rn>='||v_start;
    open t_cursor for v_sql;
    v_sql:='select count(*) from '||tableName;
    --获得总记录数
    execute immediate v_sql into totalRecord;
    --计算总页数
    if mod(totalRecord,recordPerPage)=0 then
      totalPage :=totalRecord/recordPerPage;
    else
      totalPage :=totalRecord/recordPerPage+1;
    end if;
    close t_cursor;
    end;

  • 相关阅读:
    noi2002银河英雄传说(并查集)
    Ural1076(km算法)
    km算法的个人理解
    函数之装饰器
    函数进阶(一)
    python全栈测试题(一)
    python基础之循环语句
    字符串方法总结
    python基础3
    python基础2
  • 原文地址:https://www.cnblogs.com/testing/p/2987933.html
Copyright © 2011-2022 走看看