自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

2分法-通用存儲過程分頁

數(shù)據(jù)庫
2分法-通用存儲過程分頁(top max模式)版本(性能相對之前的not in版本極大提高)

2分法-通用存儲過程分頁(top max模式)版本(性能相對之前的not in版本極大提高)

  1. --/*-----存儲過程 分頁處理 孫偉 2005-03-28創(chuàng)建 -------*/  
  2. --/*----- 對數(shù)據(jù)進(jìn)行了2分處理使查詢前半部分?jǐn)?shù)據(jù)與查詢后半部分?jǐn)?shù)據(jù)性能相同 -------*/  
  3. --/*-----存儲過程 分頁處理 孫偉 2005-04-21修改 添加Distinct查詢功能-------*/  
  4. --/*-----存儲過程 分頁處理 孫偉 2005-05-18修改 多字段排序規(guī)則問題-------*/  
  5. --/*-----存儲過程 分頁處理 孫偉 2005-06-15修改 多字段排序修改-------*/  
  6. --/*-----存儲過程 分頁處理 孫偉 2005-12-13修改 修改數(shù)據(jù)分頁方式為top max模式 性能有極大提高-------*/  
  7. --/*-----缺點:相對之前的not in版本主鍵只能是整型字段,如主鍵為GUID類型請使用not in 模式的版本-------*/  
  8. CREATE PROCEDURE dbo.proc_ListPageInt  
  9. (  
  10. @tblName     nvarchar(200),        ----要顯示的表或多個表的連接  
  11. @fldName     nvarchar(500) = '*',    ----要顯示的字段列表  
  12. @pageSize    int = 10,        ----每頁顯示的記錄個數(shù)  
  13. @page        int = 1,        ----要顯示那一頁的記錄  
  14. @pageCount    int = 1 output,            ----查詢結(jié)果分頁后的總頁數(shù)  
  15. @Counts    int = 1 output,                ----查詢到的記錄數(shù)  
  16. @fldSort    nvarchar(200) = null,    ----排序字段列表或條件  
  17. @Sort        bit = 0,        ----排序方法,0為升序,1為降序(如果是多字段排列Sort指代最后一個排序字段的排列順序(最后一個排序字段不加排序標(biāo)記)--程序傳參如:' SortA Asc,SortB Desc,SortC ')  
  18. @strCondition    nvarchar(1000) = null,    ----查詢條件,不需where  
  19. @ID        nvarchar(150),        ----主表的主鍵  
  20. @Dist                 bit = 0           ----是否添加查詢字段的 DISTINCT 默認(rèn)0不添加/1添加  
  21. )  
  22. AS  
  23. SET NOCOUNT ON  
  24. Declare @sqlTmp nvarchar(1000)        ----存放動態(tài)生成的SQL語句  
  25. Declare @strTmp nvarchar(1000)        ----存放取得查詢結(jié)果總數(shù)的查詢語句  
  26. Declare @strID     nvarchar(1000)        ----存放取得查詢開頭或結(jié)尾ID的查詢語句  
  27.  
  28. Declare @strSortType nvarchar(10)    ----數(shù)據(jù)排序規(guī)則A  
  29. Declare @strFSortType nvarchar(10)    ----數(shù)據(jù)排序規(guī)則B  
  30.  
  31. Declare @SqlSelect nvarchar(50)         ----對含有DISTINCT的查詢進(jìn)行SQL構(gòu)造  
  32. Declare @SqlCounts nvarchar(50)          ----對含有DISTINCT的總數(shù)查詢進(jìn)行SQL構(gòu)造  
  33.  
  34.  
  35. if @Dist  = 0 
  36. begin  
  37.     set @SqlSelect = 'select ' 
  38.     set @SqlCounts = 'Count(*)' 
  39. end  
  40. else  
  41. begin  
  42.     set @SqlSelect = 'select distinct ' 
  43.     set @SqlCounts = 'Count(DISTINCT '+@ID+')'  
  44. end  
  45.  
  46.  
  47. if @Sort=0 
  48. begin  
  49.     set @strFSortType=' ASC ' 
  50.     set @strSortType=' DESC ' 
  51. end  
  52. else  
  53. begin  
  54.     set @strFSortType=' DESC ' 
  55.     set @strSortType=' ASC ' 
  56. end  
  57.  
  58.  
  59.  
  60. --------生成查詢語句--------  
  61. --此處@strTmp為取得查詢結(jié)果數(shù)量的語句  
  62. if @strCondition is null or @strCondition=''     --沒有設(shè)置顯示條件  
  63. begin  
  64.     set @sqlTmp =  @fldName + ' From ' + @tblName  
  65.     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName  
  66.     set @strID = ' From ' + @tblName  
  67. end  
  68. else  
  69. begin  
  70.     set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition  
  71.     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition  
  72.     set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition  
  73. end  
  74.  
  75. ----取得查詢結(jié)果總數(shù)量-----  
  76. exec sp_executesql @strTmp,N'@Counts int out ',@Counts out  
  77. declare @tmpCounts int  
  78. if @Counts = 0 
  79.     set @tmpCounts = 1 
  80. else  
  81.     set @tmpCounts = @Counts  
  82.  
  83.     --取得分頁總數(shù)  
  84.     set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize  
  85.  
  86.     /**//**當(dāng)前頁大于總頁數(shù) 取最后一頁**/  
  87.     if @page>@pageCount  
  88.         set @page=@pageCount  
  89.  
  90.     --/*-----數(shù)據(jù)分頁2分處理-------*/  
  91.     declare @pageIndex int --總數(shù)/頁大小  
  92.     declare @lastcount int --總數(shù)%頁大小   
  93.  
  94.     set @pageIndex = @tmpCounts/@pageSize  
  95.     set @lastcount = @tmpCounts%@pageSize  
  96.     if @lastcount > 0  
  97.         set @pageIndex = @pageIndex + 1  
  98.     else  
  99.         set @lastcount = @pagesize  
  100.  
  101.     --//***顯示分頁  
  102.     if @strCondition is null or @strCondition=''     --沒有設(shè)置顯示條件  
  103.     begin  
  104.         if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分?jǐn)?shù)據(jù)處理  
  105.             begin   
  106.                 if @page=1 
  107.                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName                          
  108.                         +' order by '+ @fldSort +' '+ @strFSortType  
  109.                 else  
  110.                 begin                      
  111.                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName  
  112.                         +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName  
  113.                         +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'  
  114.                         +' order by '+ @fldSort +' '+ @strFSortType  
  115.                 end      
  116.             end  
  117.         else  
  118.             begin  
  119.             set @page = @pageIndex-@page+1 --后半部分?jǐn)?shù)據(jù)處理  
  120.                 if @page <= 1 --最后一頁數(shù)據(jù)顯示                  
  121.                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName  
  122.                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType   
  123.                 else  
  124.                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName  
  125.                         +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName  
  126.                         +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'  
  127.                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType   
  128.             end  
  129.     end  
  130.  
  131.     else --有查詢條件  
  132.     begin  
  133.         if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分?jǐn)?shù)據(jù)處理  
  134.         begin  
  135.                 if @page=1 
  136.                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName                          
  137.                         +' where 11=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType  
  138.                 else  
  139.                 begin                      
  140.                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName  
  141.                         +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName  
  142.                         +' where (11=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'  
  143.                         +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType  
  144.                 end              
  145.         end  
  146.         else  
  147.         begin   
  148.             set @page = @pageIndex-@page+1 --后半部分?jǐn)?shù)據(jù)處理  
  149.             if @page <= 1 --最后一頁數(shù)據(jù)顯示  
  150.                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName  
  151.                         +' where (11=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType                       
  152.             else  
  153.                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName  
  154.                         +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName  
  155.                         +' where (11=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'  
  156.                         +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType                  
  157.         end      
  158.     end  
  159.  
  160. ------返回查詢結(jié)果-----  
  161. exec sp_executesql @strTmp  
  162. --print @strTmp  
  163. SET NOCOUNT OFF  
  164. GO 

 調(diào)用方法列子:
 

  1. /**//// <summary> 
  2.     /// 通用分頁數(shù)據(jù)讀取函數(shù)   
  3.     /// 注意:在函數(shù)調(diào)用外部打開和關(guān)閉連接,以及關(guān)閉數(shù)據(jù)讀取器  
  4.     /// </summary> 
  5.     /// <param name="comm">SqlCommand對象</param> 
  6.     /// <param name="_tblName">查詢的表/表聯(lián)合</param> 
  7.     /// <param name="_fldName">要查詢的字段名</param> 
  8.     /// <param name="_pageSize">每頁數(shù)據(jù)大小</param> 
  9.     /// <param name="_page">當(dāng)前第幾頁</param> 
  10.     /// <param name="_fldSort">排序字段</param> 
  11.     /// <param name="_Sort">排序順序0降序1升序</param> 
  12.     /// <param name="_strCondition">過濾條件</param> 
  13.     /// <param name="_ID">主表主鍵</param>          
  14.     /// <param name="_dr">返回的SqlDataReader ref</param> 
  15.     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)  
  16.     {  
  17.         //注意:在函數(shù)調(diào)用外部打開和關(guān)閉連接,以及關(guān)閉數(shù)據(jù)讀取器  
  18.         //comm = new SqlCommand("proc_ListPage",conn);  
  19.         //comm.CommandType = CommandType.StoredProcedure;  
  20.         comm.Parameters.Add("@tblName", SqlDbType.NVarChar, 200);  
  21.         comm.Parameters["@tblName"].Value = _tblName;  
  22.         comm.Parameters.Add("@fldName", SqlDbType.NVarChar, 500);  
  23.         comm.Parameters["@fldName"].Value = _fldName;  
  24.         comm.Parameters.Add("@pageSize", SqlDbType.Int);  
  25.         comm.Parameters["@pageSize"].Value = _pageSize;  
  26.         comm.Parameters.Add("@page", SqlDbType.Int);  
  27.         comm.Parameters["@page"].Value = _page;  
  28.         comm.Parameters.Add("@fldSort", SqlDbType.NVarChar, 200);  
  29.         comm.Parameters["@fldSort"].Value = _fldSort;  
  30.         comm.Parameters.Add("@Sort", SqlDbType.Bit);  
  31.         comm.Parameters["@Sort"].Value = _Sort;  
  32.         comm.Parameters.Add("@strCondition", SqlDbType.NVarChar, 1000);  
  33.         comm.Parameters["@strCondition"].Value = _strCondition;  
  34.         comm.Parameters.Add("@ID", SqlDbType.NVarChar, 150);  
  35.         comm.Parameters["@ID"].Value = _ID;  
  36.         comm.Parameters.Add("@Counts", SqlDbType.Int, 0);  
  37.         comm.Parameters["@Counts"].Direction = ParameterDirection.Output;  
  38.         comm.Parameters.Add("@pageCount", SqlDbType.Int, 0);  
  39.         comm.Parameters["@pageCount"].Direction = ParameterDirection.Output;  
  40.  
  41.         _dr = comm.ExecuteReader();  
  42.     }  

調(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)備之用,故小編在這個就不多做推薦其他文章了。

責(zé)任編輯:艾婧 來源: 博客園
相關(guān)推薦

2010-06-10 12:37:27

MySQL分頁查詢

2010-06-11 14:41:20

MySQL分頁查詢

2011-03-24 14:15:27

雙TOP二分法分頁

2010-05-13 15:54:56

MySQL分頁查詢

2015-08-19 14:18:56

SQLasp.net后臺調(diào)用

2011-04-15 13:21:41

DB2翻頁存儲

2010-09-13 13:12:28

sqlserver分頁

2010-11-29 09:45:30

Sybase分頁

2023-12-27 23:30:50

2011-03-28 10:46:36

sql server存儲分頁

2011-10-10 16:44:37

分頁數(shù)據(jù)庫

2010-11-29 09:12:46

sybase分頁存儲過

2011-03-24 13:38:47

SQL Server 存儲分頁

2021-12-26 00:10:39

二分法排查版本

2010-10-26 14:50:11

oracle存儲過程

2018-06-15 14:26:42

2012-04-23 15:10:18

ASP.NET

2010-09-14 10:47:45

sql server存

2011-06-17 17:37:16

JavaSQL Server

2021-10-19 09:59:25

二分法排序數(shù)組
點贊
收藏

51CTO技術(shù)棧公眾號