返回> 网站首页
[转载]mssql海量高效分页存储过程
yoours2014-04-23 12:59:59
简介一边听听音乐,一边写写文章。
SQL Code
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | CREATE PROCEDURE [dbo].[PROC_GetListRecordByPage] @tblName VARCHAR(255), -- 表名 @fldName VARCHAR(255), -- 主键字段名 @PageSize INT = 10,-- 页尺寸 @PageIndex INT = 1, -- 页码 @IsReCount INT = 0, -- 0 返回记录总数; 1只返回数据; 2 都返回 @OrderType BIT = 0, -- 设置排序类型, 非 0 值则降序 @strWhere VARCHAR(3000) = '', -- 查询条件 (注意: 不要加 where) @returnFld VARCHAR(1000) = '*' --需要返回的列 AS BEGIN DECLARE @tempSql VARCHAR(6000) -- 临时语句 DECLARE @strSqlAll VARCHAR(6000) -- 主语句 DECLARE @strSqlCnt VARCHAR(400) -- 数据数量 DECLARe @OrderTypeStr NVARCHAR(20) IF @OrderType != 0 or @OrderType is null SET @OrderTypeStr = 'DESC' ELSE SET @OrderTypeStr = 'ASC' SET @tempSql = 'SELECT ROW_NUMBER() OVER (ORDER BY [' + @fldName + '] ' + @OrderTypeStr + ') AS rowId,' + @returnFld + ' FROM [' + @tblName + '] ' IF @strWhere != '' SET @tempSql = @tempSql + ' WHERE ' + @strWhere SET @strSqlAll = 'SELECT * FROM (' + @tempSql + ') t WHERE rowId BETWEEN ' + CAST(((@PageIndex - 1) * @PageSize + 1) AS VARCHAR) + ' AND ' + CAST((@PageIndex * @PageSize ) AS VARCHAR); --PRINT @strSqlAll EXEC (@strSqlAll) IF @IsReCount != 1 IF @strWhere != '' SET @strSqlCnt = 'SELECT COUNT(1) AS Total FROM [' + @tblName + '] WHERE ' + @strWhere ELSE SET @strSqlCnt = 'SELECT COUNT(1) AS Total FROM [' + @tblName + ']' EXEC (@strSqlCnt) END 或者 CREATE PROCEDURE [dbo].[PROC_GetListByPage_Sql2000] @tblName varchar(255), -- 表名 @fldName varchar(255), -- 排序字段名 随机为newid() @PageSize int = 10, -- 页尺寸 @PageIndex int = 1, -- 页码 @IsReCount int = 0, -- 0 返回记录总数; 1只返回数据; 2 都返回 @OrderType bit = 0, -- 设置排序类型, 非 0 值则降序 @strWhere varchar(3000) = '', -- 查询条件 (注意: 不要加 where) @returnFld varchar(1000) = '*' --需要返回的列 AS declare @strSQL varchar(6000) -- 主语句 declare @strSQL1 varchar(6000) -- 主语句 declare @strTmp varchar(2000) -- 临时变量 declare @strOrder varchar(400) -- 排序类型 if @OrderType != 0 begin set @strTmp = '<(select min' set @strOrder = ' order by ' + @fldName +' desc' end else begin set @strTmp = '>(select max' set @strOrder = ' order by ' + @fldName +' asc' end set @strSQL = 'select top ' + str(@PageSize) + @returnFld + ' from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)' + @strOrder if @strWhere != '' set @strSQL = 'select top ' + str(@PageSize) + @returnFld + ' from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder if @PageIndex = 1 begin set @strTmp ='' if @strWhere != '' set @strTmp = ' where ' + @strWhere set @strSQL = 'select top ' + str(@PageSize) + @returnFld + ' from [' + @tblName + ']' + @strTmp + ' ' + @strOrder end --print @strSQL exec (@strSQL) if @IsReCount != 1 if @strWhere != '' set @strSQL1 = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere else set @strSQL1 = 'select count(*) as Total from [' + @tblName + ']' exec (@strSQL1) |
SQL Code
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 高效储存过程分页: CREATE PROCEDURE [dbo].[Common] @strTable varchar(30),--表名 @strFields varchar(300),--检索的字段 @strSortField varchar(30),--排序的字段 @PageIndex int =1, --页码 @PgSize int,--每页大小 @doCount bit=0, --返回记录总数,非0则返回 @strWhere varchar(1500)='' --查询条件(注意不带where) AS declare @strSQL varchar(5000) --主语句 declare @strTmp varchar(110) --临时变量 declare @strOrder varchar(400) --排序类型 declare @tblName varchar(255) --表名 declare @strGetFields varchar(1000) --需要返回的列 declare @fidName varchar(255) --排序的字段名 declare @PageSize int --页大小 declare @OrderType bit --设置排序类型,非0则降序 set @tblName=@strTable set @strGetFields=@strFields set @fidName=@strSortField set @PageSize=@PgSize set @OrderType=1 if @doCount !=0 --返回记录总数 begin if @strWhere != '' set @strSQL=" select count(1) as Total from ["+@tblName+"] where "+@strWhere else set @strSQL=" select count(1) as Total from ["+@tblName+"]" end else begin if @OrderType !=0 begin set @strTmp="<( select min " set @strOrder = " order by ["+@fidName+"] desc" --如果@OrderType不是0,就执行降序,这句很重要! end else begin set @strTmp=">( select max " set @strOrder = " order by ["+@fidName+"] asc" end if @PageIndex=1 begin if @strWhere != '' set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where " + @strWhere + " " + @strOrder else set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["+ @tblName + "] "+ @strOrder --如果是第一页就执行以上代码,这样会加快执行速度 end else begin --以下代码赋予了@strSQL以真正执行的SQL代码 set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where [" + @fidName + "]" + @strTmp + "(["+ @fidName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fidName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder if @strWhere != '' set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where [" + @fidName + "]" + @strTmp + "([" + @fidName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " [" + @fidName + "] from [" + @tblName + "] where " + @strWhere + " " + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder end end exec(@strSQL) GO |
文章评论
1655人参与,0条评论