存储过程分页,千万数量级分页存储过程(效果演示)

千万数量级分页存储过程(效果演示) - 应用软件 - 电脑教程网

千万数量级分页存储过程(效果演示)

日期:2007-02-10   荐:
  效果演示:http://www.cn5135.com/_App/Enterprise/QueryResult.aspx

CREATE PROCEDURE CN5135_SP_Pagination/****************************************************************** 千万数量级分页存储过程 *****************************************************************参数说明:1.Tables :表名称,视图2.PrimaryKey :主关键字3.Sort :排序语句,不带Order By 比如:NewsID Desc,OrderRows Asc4.CurrentPage :当前页码5.PageSize :分页尺寸6.Filter :过滤语句,不带Where 7.Group :Group语句,不带Group By效果演示:http://www.cn5135.com/_App/Enterprise/QueryResult.aspx***************************************************************/(@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/*默认排序*/IF @Sort IS NULL OR @Sort = ''SET @Sort = @PrimaryKeyDECLARE @SortTable varchar(100)DECLARE @SortName varchar(100)DECLARE @strSortColumn varchar(200)DECLARE @operator char(2)DECLARE @type varchar(100)DECLARE @prec int/*设定排序语句.*/IF CHARINDEX('DESC',@Sort)>0BEGINSET @strSortColumn = REPLACE(@Sort, 'DESC', '')SET @operator = '<='ENDELSEBEGINIF CHARINDEX('ASC', @Sort) = 0SET @strSortColumn = REPLACE(@Sort, 'ASC', '')SET @operator = '>='ENDIF CHARINDEX('.', @strSortColumn) > 0BEGINSET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) 1, LEN(@strSortColumn))ENDELSEBEGINSET @SortTable = @TablesSET @SortName = @strSortColumnENDSELECT @type=t.name, @prec=c.precFROM sysobjects o JOIN syscolumns c on o.id=c.idJOIN systypes t on c.xusertype=t.xusertypeWHERE o.name = @SortTable AND c.name = @SortNameIF CHARINDEX('char', @type) > 0SET @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)/*默认当前页*/IF @CurrentPage < 1SET @CurrentPage = 1/*设置分页参数.*/SET @strPageSize = CAST(@PageSize AS varchar(50))SET @strStartRow = CAST(((@CurrentPage - 1)*@PageSize 1) AS varchar(50))/*筛选以及分组语句.*/IF @Filter IS NOT NULL AND @Filter != ''BEGINSET @strFilter = ' WHERE ' @Filter ' 'SET @strSimpleFilter = ' AND ' @Filter ' 'ENDELSEBEGINSET @strSimpleFilter = ''SET @strFilter = ''ENDIF @Group IS NOT NULL AND @Group != ''SET @strGroup = ' GROUP BY ' @Group ' 'ELSESET @strGroup = ''/*执行查询语句*/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 '')GO-----------------------------------------------------

标签: