自己收集比較強大的分頁存儲過程 推薦建站知識
導讀:1建站知識這兩天幫朋友修改一個項目,看到一個存儲過程,感覺性能非常高。于是,整合自己以前搜集的比較好的存儲過程,高端網站建設網站優化seo培訓。
(下面的代碼原來我想用折疊的代碼的,但是在google里面老是添加不了折疊的代碼,所以就整屏的貼出來了,望大家不要見外。) 朋友的比較好的存儲過程。優點是:性能非常的高,每次查詢都是根據ID查詢,每次都是對一半的數據進行分頁。缺點是:當有多個排序條件時,分頁數據顯示會出現問題。(該問題在第二個網友的分頁存儲過程中有解決的方法)。
復制代碼 代碼如下:
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROCEDURE [dbo].[proc_page] ( @tblName nvarchar(200), ----要顯示的表或多個表的連接lihu @fldName nvarchar(500) = '*', ----要顯示的字段列表 @pageSize int = 10, ----每頁顯示的記錄個數 @page int = 1, ----要顯示那一頁的記錄 @fldSort nvarchar(200) = null, ----排序字段列表或條件 @Sort bit = 0, ----排序方法,0為升序,1為降序(如果是多字段排列Sort指代最后一個排序字段的排列順序(最后一個排序字段不加排序標記)--程序傳參如:' SortA Asc,SortB Desc,SortC ') @strCondition nvarchar(1000), ----查詢條件,不需where @ID nvarchar(150), ----主表的主鍵 @Dist bit = 0, ----是否添加查詢字段的 DISTINCT 默認0不添加/1添加 @pageCount int = 1 output, ----查詢結果分頁后的總頁數 @Counts int = 1 output ----查詢到的記錄數 ) AS SET NOCOUNT ON Declare @sqlTmp nvarchar(1000) ----存放動態生成的SQL語句 Declare @strTmp nvarchar(1000) ----存放取得查詢結果總數的查詢語句 Declare @strID nvarchar(1000) ----存放取得查詢開頭或結尾ID的查詢語句 Declare @strSortType nvarchar(10) ----數據排序規則A Declare @strFSortType nvarchar(10) ----數據排序規則B Declare @SqlSelect nvarchar(50) ----對含有DISTINCT的查詢進行SQL構造 Declare @SqlCounts nvarchar(50) ----對含有DISTINCT的總數查詢進行SQL構造 declare @timediff datetime --耗時測試時間差 select @timediff=getdate() if @Dist = 0 begin set @SqlSelect = 'select ' set @SqlCounts = 'Count(*)' end else begin set @SqlSelect = 'select distinct ' set @SqlCounts = 'Count(DISTINCT '+@ID+')' end if @So網站推廣優化seort=0 begin set @strFSortType=' ASC ' set @strSortType=' DESC ' end else begin set @strFSortType=' DESC ' set @strSortType=' ASC ' end --------生成查詢語句-------- --此處@strTmp為取得查詢結果數量的語句 if @strCondition is null or @strCondition='' --沒有設置顯示條件 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 ----取得查詢結果總數量----- exec sp_executesql @strTmp,N'@Counts int out ',@Counts out declare @tmpCounts int if @Counts = 0 set @tmpCounts = 1 else set @tmpCounts = @Counts --取得分頁總數 set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize /**當前頁大于總頁數 取最后一頁**/ if 2>@pageCount set 2=@pageCount --/*-----數據分頁2分處理-------*/ declare @pageIndex int --總數/頁大小 declare @lastcount int --總數%頁大小 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='' --沒有設置顯示條件 begin if @pageIndex<2 or 2<=@pageIndex / 2 + @pageIndex % 2 --前半部分數據處理 begin if 2=1 set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' order by '+ @fldSort +' '+ @strFSortType else begin if @Sort=1 begin set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(2-1) as Varchar(20)) +' '+ @ID +' from '+@tblName +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' +' order by '+ @fldSort +' '+ @strFSortType end else begin set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where '+@ID+' >(select max('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(2-1) as Varchar(20)) +' '+ @ID +' 網站seo優化from '+@tblName +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' +' order by '+ @fldSort +' '+ @strFSortType end end end else begin set 2= @pageIndex-2+1 --后半部分數據處理 if 2<= 1 --最后一頁數據顯示 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 if @Sort=1 begin set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(2-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 else begin set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where '+@ID+' <(select min('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(2-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 end else --有查詢條件 begin if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分數據處理 begin if @page=1 set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType else if(@Sort=1) 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 (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType end else begin set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where '+@ID+' >(select max('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType end end else begin set 2= @pageIndex-2+1 --后半部分數據處理 if 2<= 1 --最后一頁數據顯示 set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType else if(@Sort=1) 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 (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' +' '+ @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+' <(sele企業網站建設ct min('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType end end ------返回查詢結果----- exec sp_executesql @strTmp select datediff(ms,@timediff,getdate()) as 耗時 print @strTmp SET NOCOUNT OFF
聲明: 本文由我的SEOUC技術文章主頁發布于:2023-05-22 ,文章自己收集比較強大的分頁存儲過程 推薦建站知識主要講述分頁,存儲過程,自己收集比較強大的分頁存儲過程網站建設源碼以及服務器配置搭建相關技術文章。轉載請保留鏈接: http://www.bifwcx.com/article/web_4859.html