2分法-通用存儲過程分頁
2分法-通用存儲過程分頁(top max模式)版本(性能相對之前的not in版本極大提高)
- --/*-----存儲過程 分頁處理 孫偉 2005-03-28創(chuàng)建 -------*/
- --/*----- 對數(shù)據(jù)進(jìn)行了2分處理使查詢前半部分?jǐn)?shù)據(jù)與查詢后半部分?jǐn)?shù)據(jù)性能相同 -------*/
- --/*-----存儲過程 分頁處理 孫偉 2005-04-21修改 添加Distinct查詢功能-------*/
- --/*-----存儲過程 分頁處理 孫偉 2005-05-18修改 多字段排序規(guī)則問題-------*/
- --/*-----存儲過程 分頁處理 孫偉 2005-06-15修改 多字段排序修改-------*/
- --/*-----存儲過程 分頁處理 孫偉 2005-12-13修改 修改數(shù)據(jù)分頁方式為top max模式 性能有極大提高-------*/
- --/*-----缺點:相對之前的not in版本主鍵只能是整型字段,如主鍵為GUID類型請使用not in 模式的版本-------*/
- CREATE PROCEDURE dbo.proc_ListPageInt
- (
- @tblName nvarchar(200), ----要顯示的表或多個表的連接
- @fldName nvarchar(500) = '*', ----要顯示的字段列表
- @pageSize int = 10, ----每頁顯示的記錄個數(shù)
- @page int = 1, ----要顯示那一頁的記錄
- @pageCount int = 1 output, ----查詢結(jié)果分頁后的總頁數(shù)
- @Counts int = 1 output, ----查詢到的記錄數(shù)
- @fldSort nvarchar(200) = null, ----排序字段列表或條件
- @Sort bit = 0, ----排序方法,0為升序,1為降序(如果是多字段排列Sort指代最后一個排序字段的排列順序(最后一個排序字段不加排序標(biāo)記)--程序傳參如:' SortA Asc,SortB Desc,SortC ')
- @strCondition nvarchar(1000) = null, ----查詢條件,不需where
- @ID nvarchar(150), ----主表的主鍵
- @Dist bit = 0 ----是否添加查詢字段的 DISTINCT 默認(rèn)0不添加/1添加
- )
- AS
- SET NOCOUNT ON
- Declare @sqlTmp nvarchar(1000) ----存放動態(tài)生成的SQL語句
- Declare @strTmp nvarchar(1000) ----存放取得查詢結(jié)果總數(shù)的查詢語句
- Declare @strID nvarchar(1000) ----存放取得查詢開頭或結(jié)尾ID的查詢語句
- Declare @strSortType nvarchar(10) ----數(shù)據(jù)排序規(guī)則A
- Declare @strFSortType nvarchar(10) ----數(shù)據(jù)排序規(guī)則B
- Declare @SqlSelect nvarchar(50) ----對含有DISTINCT的查詢進(jìn)行SQL構(gòu)造
- Declare @SqlCounts nvarchar(50) ----對含有DISTINCT的總數(shù)查詢進(jìn)行SQL構(gòu)造
- if @Dist = 0
- begin
- set @SqlSelect = 'select '
- set @SqlCounts = 'Count(*)'
- end
- else
- begin
- set @SqlSelect = 'select distinct '
- set @SqlCounts = 'Count(DISTINCT '+@ID+')'
- end
- if @Sort=0
- begin
- set @strFSortType=' ASC '
- set @strSortType=' DESC '
- end
- else
- begin
- set @strFSortType=' DESC '
- set @strSortType=' ASC '
- end
- --------生成查詢語句--------
- --此處@strTmp為取得查詢結(jié)果數(shù)量的語句
- if @strCondition is null or @strCondition='' --沒有設(shè)置顯示條件
- begin
- set @sqlTmp = @fldName + ' From ' + @tblName
- set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
- set @strID = ' From ' + @tblName
- end
- else
- begin
- set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
- set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
- set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
- end
- ----取得查詢結(jié)果總數(shù)量-----
- exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
- declare @tmpCounts int
- if @Counts = 0
- set @tmpCounts = 1
- else
- set @tmpCounts = @Counts
- --取得分頁總數(shù)
- set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
- /**//**當(dāng)前頁大于總頁數(shù) 取最后一頁**/
- if @page>@pageCount
- set @page=@pageCount
- --/*-----數(shù)據(jù)分頁2分處理-------*/
- declare @pageIndex int --總數(shù)/頁大小
- declare @lastcount int --總數(shù)%頁大小
- set @pageIndex = @tmpCounts/@pageSize
- set @lastcount = @tmpCounts%@pageSize
- if @lastcount > 0
- set @pageIndex = @pageIndex + 1
- else
- set @lastcount = @pagesize
- --//***顯示分頁
- if @strCondition is null or @strCondition='' --沒有設(shè)置顯示條件
- begin
- if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分?jǐn)?shù)據(jù)處理
- begin
- if @page=1
- set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
- +' order by '+ @fldSort +' '+ @strFSortType
- else
- begin
- set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
- +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
- +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
- +' order by '+ @fldSort +' '+ @strFSortType
- end
- end
- else
- begin
- set @page = @pageIndex-@page+1 --后半部分?jǐn)?shù)據(jù)處理
- if @page <= 1 --最后一頁數(shù)據(jù)顯示
- set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
- +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
- else
- set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
- +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
- +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
- +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
- end
- end
- else --有查詢條件
- begin
- if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分?jǐn)?shù)據(jù)處理
- begin
- if @page=1
- set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
- +' where 11=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType
- else
- begin
- set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
- +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
- +' where (11=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
- +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType
- end
- end
- else
- begin
- set @page = @pageIndex-@page+1 --后半部分?jǐn)?shù)據(jù)處理
- if @page <= 1 --最后一頁數(shù)據(jù)顯示
- set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
- +' where (11=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
- else
- set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
- +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
- +' where (11=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
- +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
- end
- end
- ------返回查詢結(jié)果-----
- exec sp_executesql @strTmp
- --print @strTmp
- SET NOCOUNT OFF
- GO
調(diào)用方法列子:
- /**//// <summary>
- /// 通用分頁數(shù)據(jù)讀取函數(shù)
- /// 注意:在函數(shù)調(diào)用外部打開和關(guān)閉連接,以及關(guān)閉數(shù)據(jù)讀取器
- /// </summary>
- /// <param name="comm">SqlCommand對象</param>
- /// <param name="_tblName">查詢的表/表聯(lián)合</param>
- /// <param name="_fldName">要查詢的字段名</param>
- /// <param name="_pageSize">每頁數(shù)據(jù)大小</param>
- /// <param name="_page">當(dāng)前第幾頁</param>
- /// <param name="_fldSort">排序字段</param>
- /// <param name="_Sort">排序順序0降序1升序</param>
- /// <param name="_strCondition">過濾條件</param>
- /// <param name="_ID">主表主鍵</param>
- /// <param name="_dr">返回的SqlDataReader ref</param>
- public static void CutPageData(SqlConnection conn, ref SqlCommand comm, string _tblName, string _fldName, int _pageSize, int _page, string _fldSort, int _Sort, string _strCondition, string _ID, ref SqlDataReader _dr)
- {
- //注意:在函數(shù)調(diào)用外部打開和關(guān)閉連接,以及關(guān)閉數(shù)據(jù)讀取器
- //comm = new SqlCommand("proc_ListPage",conn);
- //comm.CommandType = CommandType.StoredProcedure;
- comm.Parameters.Add("@tblName", SqlDbType.NVarChar, 200);
- comm.Parameters["@tblName"].Value = _tblName;
- comm.Parameters.Add("@fldName", SqlDbType.NVarChar, 500);
- comm.Parameters["@fldName"].Value = _fldName;
- comm.Parameters.Add("@pageSize", SqlDbType.Int);
- comm.Parameters["@pageSize"].Value = _pageSize;
- comm.Parameters.Add("@page", SqlDbType.Int);
- comm.Parameters["@page"].Value = _page;
- comm.Parameters.Add("@fldSort", SqlDbType.NVarChar, 200);
- comm.Parameters["@fldSort"].Value = _fldSort;
- comm.Parameters.Add("@Sort", SqlDbType.Bit);
- comm.Parameters["@Sort"].Value = _Sort;
- comm.Parameters.Add("@strCondition", SqlDbType.NVarChar, 1000);
- comm.Parameters["@strCondition"].Value = _strCondition;
- comm.Parameters.Add("@ID", SqlDbType.NVarChar, 150);
- comm.Parameters["@ID"].Value = _ID;
- comm.Parameters.Add("@Counts", SqlDbType.Int, 0);
- comm.Parameters["@Counts"].Direction = ParameterDirection.Output;
- comm.Parameters.Add("@pageCount", SqlDbType.Int, 0);
- comm.Parameters["@pageCount"].Direction = ParameterDirection.Output;
- _dr = comm.ExecuteReader();
- }
調(diào)用例如:
CutPageData(conn, ref comm, "VOX_CDSinger", "id, cdsinger, cdsingertype, area, cdsingerreadme", 15, page, "id", 1, strFilter, "id", ref dr);
對應(yīng)說明:
CutPageData(數(shù)據(jù)連接對象, ref Sqlcommand對象, "需要表或視圖名稱", "要查詢的字段", 每頁讀取數(shù)據(jù)條數(shù), 當(dāng)前頁, "排序字段可多字段如(addtime desc, visitcounts注意這里最后一個字段不加desc或asc 最后一個字段對應(yīng)于后面的排序規(guī)則)", 排序方式(1 desc 0 asc), where條件(這里不再添加where條件添加如:' and visitcounts>100'), 表主鍵, ref 返回的SqlDataReader對象);
這里的調(diào)用同樣適用于之前的not in版本.
編者注:這篇文章是為了另一篇文章做準(zhǔn)備之用,故小編在這個就不多做推薦其他文章了。