Use [Caili] Go /****** object:storedprocedure [dbo]. [Sqlpagination] Script date:10/26/2011 11:40:46 ******/ SET ANSI_NULLS on Go SET QUOTED_IDENTIFIER ON Go CREATE PROCEDURE [dbo]. [Sqlpagination] /* *************************************************************** * * Tens of millions of levels of paging stored procedures * * *************************************************************** Parameter description: 1.Tables: Table name, view 2.PrimaryKey: Primary keyword 3.Sort: Sort statements without ORDER by example: NewsID desc,orderrows ASC 4.CurrentPage: Current page number 5.PageSize: Paging Size 6.Filter: Filter statements, without where 7.group:group statement without Group by ***************************************************************/ ( @Tables varchar (1000), @PrimaryKey varchar (100), @Sort varchar = NULL, @CurrentPage int = 1, @PageSize int = 10, @Fields varchar (1000) = ' * ', @Filter varchar (1000) = NULL, @Group varchar (1000) = NULL ) As /* Default sort */ If @PrimaryKey is NULL or @PrimaryKey = ' Set @PrimaryKey = ' ID ' IF @Sort is NULL or @Sort = ' SET @Sort = @PrimaryKey IF @Fields is NULL or @Fields = ' SET @Fields = ' * ' DECLARE @SortTable varchar (100) DECLARE @SortName varchar (100) DECLARE @strSortColumn varchar (200) DECLARE @operator char (2) DECLARE @type varchar (100) DECLARE @prec int /* Set the sort statement. * * If Charindex (', ', @Sort) >0 Set @strSortColumn = substring (@Sort, 0,charindex (', ', @Sort)) Else Set @strSortColumn = @Sort IF CHARINDEX (' DESC ', @Sort) >0 BEGIN SET @strSortColumn = REPLACE (@strSortColumn, ' DESC ', ') SET @operator = ' <= ' End ELSE BEGIN IF CHARINDEX (' ASC ', @Sort) > 0 BEGIN SET @strSortColumn = REPLACE (@strSortColumn, ' ASC ', ') SET @operator = ' >= ' End 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 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 IF CHARINDEX (' char ', @type) > 0 SET @type = @type + ' (' + CAST (@prec as varchar) + ') ' DECLARE @strPageSize varchar (50) DECLARE @strStartRow varchar (50) DECLARE @strFilter varchar (1000) DECLARE @strSimpleFilter varchar (1000) DECLARE @strGroup varchar (1000) DECLARE @strSort varchar (200) /* Default Current Page * * IF @CurrentPage < 1 SET @CurrentPage = 1 /* Set Paging parameters. * * SET @strPageSize = CAST (@PageSize as varchar (50)) SET @strStartRow = CAST ((@CurrentPage-1) * @PageSize + 1) as varchar (50)) /* Filter and GROUP statements. * * IF @Filter is not NULL and @Filter!= ' BEGIN SET @strFilter = ' Where 1=1 ' + @Filter + ' SET @strSimpleFilter = @Filter + "" End ELSE BEGIN SET @strSimpleFilter = ' SET @strFilter = ' End IF @Group is not NULL and @Group!= ' SET @strGroup = ' GROUP by ' + @Group + ' ELSE SET @strGroup = ' IF @Sort is not NULL and @Sort!= ' SET @strSort = ' ORDER BY ' + @Sort + ' ELSE SET @strSort = ' --print (' select ' + @Fields + ' from ' + ' (select *,row_number () over (' + @strSort + ') as RowNumber from ' + @Tables + ') t ' + ' Where t.rownumber between ' + @strStartRow + ' and ' + ' + @strSimpleFilter + ' + @strSort + @strGroup) /* Execute Query statement * * DECLARE @STRORDER varchar (50) If CHARINDEX (', ', @strSort) >0 Set @STRORDER =substring (@strSort, 0, CHARINDEX (', ', @strSort)) Else Set @STRORDER = @strSort EXEC ( ' DECLARE @SortColumn ' + @type + ' DECLARE @TotalCount int DECLARE @ENDCOUNT int DECLARE @strENDCOUNT varchar (50) --select count (1) from ' + @Tables + @strFilter + ' Set @TotalCount = (Select count (1) from ' + @Tables + @strFilter + ') ' + ' SET RowCount ' + @strStartRow + ' SET @ENDCOUNT =cast (' + @strStartRow + ' as int) +cast (' + @strPageSize + ' as int)-1 IF @ENDCOUNT > @TotalCount BEGIN SET @ENDCOUNT = @TotalCount End Set @strENDCOUNT =cast (@ENDCOUNT as varchar (50)) Select @SortColumn = ' + @strSortColumn + ' from ' + @Tables + @strFilter + ' + @strGroup + @strSort + ' SET RowCount ' + @strPageSize + ' Select ' + @Fields + ' from ' + ' (select *,row_number () over (' + @STRORDER + ') as RowNumber from ' + @Tables + ' where 1=1 ' + @st rsimplefilter+ ') t ' + ' Where t.rownumber between ' + @strStartRow + ' and @strENDCOUNT ' + @strGroup + @strSort + ') Go |