隨著信息時代的發展信息系統的使用越來越多,信息量越來越大;當信息量越來越大,這在數據展示,特別是報表這塊對系統展現效率要求越來越高,對於千萬級數據量的展示必須得使用分頁來展示。
If object_id('SP_Pagination')is not null
drop proc SP_Pagination
go
Create PROCEDURE SP_Pagination
/**//*
***************************************************************
** 千萬數量級分頁存儲過程 **
***************************************************************
參數說明:
.Tables :表名稱,視圖(試圖這邊目前還有點小問題)
.PrimaryKey :主關鍵字
.Sort :排序語句,不帶Order By 比如:NewsID Desc,OrderRows Asc
.CurrentPage :當前頁碼
.PageSize :分頁尺寸
.Filter :過濾語句,不帶Where
.Group :Group語句,不帶Group By
***************************************************************/
(
@Tables varchar(2000),
@PrimaryKey varchar(500),
@Sort varchar(500) = NULL,
@CurrentPage int = 1,
@PageSize int = 5,
@Fields varchar(2000) = '*',
@Filter varchar(1000) = NULL,
@Group varchar(1000) = NULL
)
AS
/**//*默認排序*/
IF @Sort IS NULL OR @Sort = ''
SET @Sort = @PrimaryKey
DECLARE @SortTable varchar(1000)
DECLARE @SortName varchar(1000)
DECLARE @strSortColumn varchar(1000)
DECLARE @operator char(2)
DECLARE @type varchar(1000)
DECLARE @prec int
/**//*設定排序語句.*/
IF CHARINDEX('DESC',@Sort)>0
BEGIN
SET @strSortColumn = REPLACE(@Sort, 'DESC', '')
SET @operator = '<='
END
ELSE
BEGIN
IF CHARINDEX('ASC', @Sort) = 0
print '1'
print REPLACE(@Sort, 'ASC', '')
SET @strSortColumn = REPLACE(@Sort, 'ASC', '')
print @strSortColumn
SET @operator = '>='
print @operator
END
IF CHARINDEX('.', @strSortColumn) > 0
BEGIN
SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn))
END
ELSE
BEGIN
SET @SortTable = @Tables
SET @SortName = @strSortColumn
print @SortTable
print @SortName
END
SELECT @type=t.name, @prec=c.prec
FROM sysobjects o
JOIN syscolumns c on o.id=c.id
JOIN systypes t on c.xusertype=t.xusertype
WHERE o.name = @SortTable AND c.name = @SortName
--print @type
--print @prec
IF CHARINDEX('char', @type) > 0
SET @type = @type + '(' + CAST(@prec AS varchar) + ')'
DECLARE @strPageSize varchar(500)
DECLARE @strStartRow varchar(500)
DECLARE @strFilter varchar(1000)
DECLARE @strSimpleFilter varchar(1000)
DECLARE @strGroup varchar(1000)
/**//*默認當前頁*/
IF @CurrentPage < 1
SET @CurrentPage = 1
/**//*設置分頁參數.*/
SET @strPageSize = CAST(@PageSize AS varchar(500))
SET @strStartRow = CAST(((@CurrentPage - 1)*@PageSize + 1) AS varchar(500))
/**//*篩選以及分組語句.*/
IF @Filter IS NOT NULL AND @Filter != ''
BEGIN
SET @strFilter = ' WHERE ' + @Filter + ' '
SET @strSimpleFilter = ' AND ' + @Filter + ' '
END
ELSE
BEGIN
SET @strSimpleFilter = ''
SET @strFilter = ''
END
IF @Group IS NOT NULL AND @Group != ''
SET @strGroup = ' GROUP BY ' + @Group + ' '
ELSE
SET @strGroup = ''
/*print @type
print @strStartRow
print @strSortColumn
print @Tables
print @strFilter
print @strGroup
print @Sort*/
/**//*執行查詢語句*/
EXEC(
'
DECLARE @SortColumn ' + @type + '
SET ROWCOUNT ' + @strStartRow + '
SELECT @SortColumn=' + @strSortColumn + ' FROM ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
SET ROWCOUNT ' + @strPageSize + '
SELECT ' + @Fields + ' FROM ' + @Tables + ' WHERE ' + @strSortColumn + @operator + ' @SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort + '
'
)
下面是在數據庫查詢分析器裡面怎麼調用這個存儲過程的實例。如圖所示為Traffic_Sites原表查詢的結果集。 以Traffic_Sites表為例執行分頁存儲過程腳本:exec SP_Pagination 'Traffic_Sites','Id','Id asc',1,5,'*','','' 執行上述SQL結果如圖所示: