通用多表分页存储过程
生活随笔
收集整理的這篇文章主要介紹了
通用多表分页存储过程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
CREATE PROCEDURE [dbo].[GetRecordByPage]
(
@tblName nvarchar(400), ----要顯示的表或多個表的連接
@fldName nvarchar(4000) = '*', ----要顯示的字段列表
@pageSize int = 1, ----每頁顯示的記錄個數
@page int = 10, ----要顯示那一頁的記錄
@pageCount int = 1 output, ----查詢結果分頁后的總頁數
@Counts int = 1 output, ----查詢到的記錄數
@fldSort nvarchar(200) = null, ----排序字段列表或條件
@Sort bit = 1, ----排序方法,0為升序,1為降序(如果是多字段排列Sort指代最后一個排序字段的排列順序(最后一個排序字段不加排序標記)--程序傳參如:' SortA Asc,SortB Desc,SortC ')
@strCondition nvarchar(4000) = null, ----查詢條件,不需where
@ID nvarchar(150), ----主表的主鍵
@Dist bit = 0 ----是否添加查詢字段的 DISTINCT 默認0不添加/1添加
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(4000) ----存放動態生成的SQL語句
Declare @strTmp nvarchar(4000) ----存放取得查詢結果總數的查詢語句
Declare @strID nvarchar(4000) ----存放取得查詢開頭或結尾ID的查詢語句Declare @strSortType nvarchar(10) ----數據排序規則A
Declare @strFSortType nvarchar(10) ----數據排序規則BDeclare @SqlSelect nvarchar(50) ----對含有DISTINCT的查詢進行SQL構造
Declare @SqlCounts nvarchar(50) ----對含有DISTINCT的總數查詢進行SQL構造if @Dist = 0
beginset @SqlSelect = 'select 'set @SqlCounts = 'Count(*)'
end
else
beginset @SqlSelect = 'select distinct 'set @SqlCounts = 'Count(DISTINCT '+@ID+')'
endif @Sort=0
beginset @strFSortType=' ASC 'set @strSortType=' DESC '
end
else
beginset @strFSortType=' DESC 'set @strSortType=' ASC '
end--------生成查詢語句--------if @strCondition is null or @strCondition='' --沒有設置顯示條件
beginset @sqlTmp = @fldName + ' From ' + @tblNameset @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblNameset @strID = ' From ' + @tblName
end
else
beginset @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strConditionset @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strConditionset @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
end----取得查詢結果總數量-----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts outdeclare @tmpCounts int
if @Counts = 0set @tmpCounts = 1
elseset @tmpCounts = @Counts--取得分頁總數set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize/**//**當前頁大于總頁數 取最后一頁**/if @page>@pageCountset @page=@pageCount--/*-----數據分頁2分處理-------*/declare @pageIndex int --總數/頁大小declare @lastcount int --總數%頁大小 set @pageIndex = @tmpCounts/@pageSizeset @lastcount = @tmpCounts%@pageSizeif @lastcount > 0set @pageIndex = @pageIndex + 1elseset @lastcount = @pagesize--//***顯示分頁if @strCondition is null or @strCondition='' --沒有設置顯示條件beginif @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分數據處理begin set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName+' order by '+ @fldSort +' '+ @strFSortType+')'+' order by '+ @fldSort +' '+ @strFSortType endelsebeginset @page = @pageIndex-@page+1 --后半部分數據處理if @page <= 1 --最后一頁數據顯示set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName+' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortTypeelse set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName+' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortTypeendendelse --有查詢條件beginif @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分數據處理begin set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName +' from '+@tblName+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName+' Where (1>0) ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType+')'+' ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType endelsebegin set @page = @pageIndex-@page+1 --后半部分數據處理if @page <= 1 --最后一頁數據顯示set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName+' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortTypeelseset @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName+' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+')'+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType end end------返回查詢結果-----exec sp_executesql @strTmpSET NOCOUNT OFF;SET NOEXEC OFF;
轉載于:https://www.cnblogs.com/Evil_Smile/archive/2011/02/14/1954547.html
總結
以上是生活随笔為你收集整理的通用多表分页存储过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EXCEL 中找出两个sheet相同列
- 下一篇: 纯CSS后台框架