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 paging stored procedures ** **************************************** *********************** Parameter description: 1. Tables: Table Name, View 2. PrimaryKey: Primary Key 3. Sort: Sorting statement without Order By such as NewsID Desc and OrderRows Asc 4. CurrentPage: Current page number 5. PageSize: page size 6. Filter: Filter statement without Where 7. Group: Group statement without Group **************************************** ***********************/ ( @ Tables varchar (1000 ), PrimaryKey varchar (100 ), @ Sort varchar (200) = NULL, @ CurrentPage int = 1, @ PageSize int = 10, @ Fields varchar (1000) = '*', @ Filter varchar (1000) = NULL, @ Group varchar (1000) = NULL ) AS /* Default sorting */ 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 sorting 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 policypes 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 statement .*/ 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 the query statement */ Declare @ STRORDER varchar (50) If CHARINDEX (',', @ strSort)> 0 Set @ STRORDER = SUBSTRING (@ strSort, 0, CHARINDEX (',', @ strSort )) Else Set @ STRORDER = @ strSort EXEC (
'Destare @ 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' + @ strSimpleFilter +') t' + 'where t. rowNumber between '+ @ strStartRow +' and @ strENDCOUNT '+ @ strGroup + @ strSort + '') GO |