首页 > 代码库 > oracle存储过程分页

oracle存储过程分页

 

 

CREATE OR REPLACE PROCEDURE prc_query       (p_tableName        in  varchar2,   --表名        p_strWhere         in  varchar2,   --查询条件        p_orderColumn      in  varchar2,   --排序的列        p_orderStyle       in  varchar2,   --排序方式        p_curPage          in out Number,  --当前页        p_pageSize         in out Number,  --每页显示记录条数        p_totalRecords     out Number,     --总记录数        p_totalPages       out Number,     --总页数        v_cur              out pkg_query.cur_query)   --返回的结果集IS   v_sql VARCHAR2(1000) := ‘‘;      --sql语句   v_startRecord Number(4);         --开始显示的记录条数   v_endRecord Number(4);           --结束显示的记录条数BEGIN   --记录中总记录条数   v_sql := SELECT TO_NUMBER(COUNT(*)) FROM  || p_tableName ||  WHERE 1=1;   IF p_strWhere IS NOT NULL or p_strWhere <> ‘‘ THEN       v_sql := v_sql || p_strWhere;   END IF;   EXECUTE IMMEDIATE v_sql INTO p_totalRecords;      --验证页面记录大小   IF p_pageSize < 0 THEN       p_pageSize := 0;   END IF;      --根据页大小计算总页数   IF MOD(p_totalRecords,p_pageSize) = 0 THEN       p_totalPages := p_totalRecords / p_pageSize;   ELSE       p_totalPages := p_totalRecords / p_pageSize + 1;   END IF;      --验证页号   IF p_curPage < 1 THEN       p_curPage := 1;   END IF;   IF p_curPage > p_totalPages THEN       p_curPage := p_totalPages;   END IF;      --实现分页查询   v_startRecord := (p_curPage - 1) * p_pageSize + 1;   v_endRecord := p_curPage * p_pageSize;   v_sql := SELECT * FROM (SELECT A.*, rownum r FROM  ||            (SELECT * FROM  || p_tableName;   IF p_strWhere IS NOT NULL or p_strWhere <> ‘‘ THEN       v_sql := v_sql ||  WHERE 1=1 || p_strWhere;   END IF;   IF p_orderColumn IS NOT NULL or p_orderColumn <> ‘‘ THEN       v_sql := v_sql ||  ORDER BY  || p_orderColumn ||   || p_orderStyle;   END IF;   v_sql := v_sql || ) A WHERE rownum <=  || v_endRecord || ) B WHERE r >=             || v_startRecord;   DBMS_OUTPUT.put_line(v_sql);   OPEN v_cur FOR v_sql;END prc_query;

 

 3.在数据层中调用存储过程,返回一个datatable及二个参数,datatable存储数据,curPage为当前页码(存储过程会根据传入的数值查询数据,返回datatable。),pageCount为总页数, RecordCount为数据总条数

public class Paging    {        /// <summary>        ///         /// </summary>        /// <param name="tName">表 名</param>        /// <param name="strSql">查询条件</param>        /// <param name="sortRow">排序的列</param>        /// <param name="sortMethod">排序方式</param>        /// <param name="curPage">当前页</param>        /// <param name="pageSize">每页显示记录条数</param>        /// <param name="pageCount">总页数</param>        /// <param name="RecordCount">总记录数</param>        /// <returns></returns>        public DataTable QuickPage(string tName, string strSql, string sortRow, string sortMethod, int curPage, int pageSize, ref int pageCount, ref int RecordCount)        {            OracleConnection conn = new OracleConnection("server=.;Data Source=db;User ID=zsxl1;Password=zsxl;");            OracleCommand cmd = new OracleCommand();            cmd.Connection = conn;            cmd.CommandText = "prc_query";            cmd.CommandType = CommandType.StoredProcedure;            cmd.Parameters.Add("p_tableName", OracleType.VarChar, 50); //表 名            cmd.Parameters["p_tableName"].Direction = ParameterDirection.Input;            cmd.Parameters["p_tableName"].Value = http://www.mamicode.com/tName;"p_strWhere", OracleType.VarChar, 3000); //查询条件            cmd.Parameters["p_strWhere"].Direction = ParameterDirection.Input;            cmd.Parameters["p_strWhere"].Value = http://www.mamicode.com/strSql;"p_orderColumn", OracleType.VarChar, 3000); //排序的列            cmd.Parameters["p_orderColumn"].Direction = ParameterDirection.Input;            cmd.Parameters["p_orderColumn"].Value = http://www.mamicode.com/sortRow;"p_orderStyle", OracleType.VarChar, 3000); //排序方式            cmd.Parameters["p_orderStyle"].Direction = ParameterDirection.Input;            cmd.Parameters["p_orderStyle"].Value = http://www.mamicode.com/sortMethod;"p_curPage", OracleType.Number); //当前页            cmd.Parameters["p_curPage"].Direction = ParameterDirection.Input;            cmd.Parameters["p_curPage"].Value = http://www.mamicode.com/curPage;"p_pageSize", OracleType.Number); //每页显示记录条数            cmd.Parameters["p_pageSize"].Direction = ParameterDirection.Input;            cmd.Parameters["p_pageSize"].Value = http://www.mamicode.com/pageSize;"p_totalRecords", OracleType.Number); //总记录数            cmd.Parameters["p_totalRecords"].Direction = ParameterDirection.Output;            cmd.Parameters["p_totalRecords"].Value = http://www.mamicode.com/0;"p_totalPages", OracleType.Number); //总页数            cmd.Parameters["p_totalPages"].Direction = ParameterDirection.Output;            cmd.Parameters["p_totalPages"].Value = http://www.mamicode.com/0;"v_cur", OracleType.Cursor); //返回的游标            cmd.Parameters["v_cur"].Direction = ParameterDirection.Output;            DataSet Ds = new DataSet();            OracleDataAdapter adapter = new OracleDataAdapter(cmd);            adapter.Fill(Ds);            conn.Close();            //总记录数            RecordCount = int.Parse(cmd.Parameters["p_totalRecords"].Value.ToString());            //总页数            pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(cmd.Parameters["p_totalPages"].Value.ToString())));            //当前页码            //curPage = int.Parse(cmd.Parameters["p_curPage"].Value.ToString());            //TotalPages = int.Parse(cmd.Parameters["p_totalPages"].Value.ToString());            return Ds.Tables[0];        }    }

4.表现层调用方法QuickPage

。。。

 

 

oracle存储过程分页