标签:exec 字段名 过程 color pre over procedure div number
因项目需要,一次性查询出来的数据过大,内存hold不住,所以特意写成分页查询,减小占用内存。
存储过程如下:
USE [XXX] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[GetDataPage] -- 获得某一页的数据 -- @currPage int = 1, --当前页页码 (即Top currPage) @showColumn varchar(2000) = ‘*‘, --需要得到的字段 (即 column1,column2,......) @tabName varchar(2000), --需要查看的表名 (即 from table_name) @strCondition varchar(2000) = ‘‘, --查询条件 (即 where condition......) 不用加where关键字 @ascColumn varchar(100) = ‘‘, --排序的字段名 (即 order by column asc/desc) @bitOrderType bit = 0, --排序的类型 (0为升序,1为降序) @pkColumn varchar(50) = ‘‘, --主键名称 @pageSize int = 20 --分页大小 AS BEGIN -- 存储过程开始 -- 该存储过程需要用到的几个变量 -- DECLARE @strTemp varchar(1000); DECLARE @strSql varchar(4000); --该存储过程最后执行的语句 DECLARE @strOrderType varchar(1000); --排序类型语句 (order by column asc或者order by column desc) IF @bitOrderType = 1 -- bitOrderType=1即执行降序 BEGIN SET @strOrderType = ‘ ORDER BY ‘+@ascColumn+‘ DESC‘ SET @strTemp = ‘<(SELECT min‘ END ELSE BEGIN SET @strOrderType = ‘ ORDER BY ‘+@ascColumn+‘ ASC‘ SET @strTemp = ‘>(SELECT max‘ END IF @currPage = 1 -- 如果是第一页 BEGIN IF @strCondition != ‘‘ SET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+‘ ‘+@showColumn+‘ FROM ‘+@tabName+ ‘ WHERE ‘+@strCondition+@strOrderType ELSE SET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+‘ ‘+@showColumn+‘ FROM ‘+@tabName+@strOrderType END ELSE -- 其他页 BEGIN IF @strCondition !=‘‘ SET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+‘ ‘+@showColumn+‘ FROM ‘+@tabName+ ‘ WHERE ‘+@strCondition+‘ AND ‘+@pkColumn+@strTemp+‘(‘+@pkColumn+‘)‘+‘ FROM (SELECT TOP ‘+STR((@currPage-1)*@pageSize)+ ‘ ‘+@pkColumn+‘ FROM ‘+@tabName+‘ WHERE ‘+@strCondition+@strOrderType+‘) AS TabTemp)‘+@strOrderType ELSE SET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+‘ ‘+@showColumn+‘ FROM ‘+@tabName+ ‘ WHERE ‘+@pkColumn+@strTemp+‘(‘+@pkColumn+‘)‘+‘ FROM (SELECT TOP ‘+STR((@currPage-1)*@pageSize)+‘ ‘+@pkColumn+ ‘ FROM ‘+@tabName+@strOrderType+‘) AS TabTemp)‘+@strOrderType END EXEC (@strSql) END -- 存储过程结束 ------------------------------------------------ GO
在网上找资料时看见用ROWNUMBER分页效率会更高,但是在测试过程中发现每页所取的数据汇总时有问题,具体什么引起的也没找到原因。【郁闷( ˇ?ˇ )】,
不过还是贴上代码,如果有一样情况的可以交流交流。
USE [XXX] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[GetDataPage] -- 获得某一页的数据 -- @currPage int = 1, --当前页页码 (即Top currPage) @showColumn varchar(2000) = ‘*‘, --需要得到的字段 (即 column1,column2,......) @tabName varchar(2000), --需要查看的表名 (即 from table_name) @strCondition varchar(2000) = ‘‘, --查询条件 (即 where condition......) 不用加where关键字 @ascColumn varchar(100) = ‘‘, --排序的字段名 (即 order by column asc/desc) @bitOrderType bit = 0, --排序的类型 (0为升序,1为降序) @pageSize int = 20 --分页大小 AS BEGIN -- 存储过程开始 -- 该存储过程需要用到的几个变量 -- DECLARE @strSql varchar(4000) --该存储过程最后执行的语句 DECLARE @strOrderType varchar(1000) --排序类型语句 (order by column asc或者order by column desc) IF @bitOrderType = 1 -- bitOrderType=1即执行降序 BEGIN SET @strOrderType = ‘ ORDER BY ‘+@ascColumn+‘ DESC‘ END ELSE BEGIN SET @strOrderType = ‘ ORDER BY ‘+@ascColumn+‘ ASC‘ END IF @currPage = 1 -- 如果是第一页 BEGIN IF @strCondition != ‘‘ SET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+‘ ‘+@showColumn+‘ FROM ‘+@tabName+ ‘ WHERE ‘+@strCondition+@strOrderType ELSE SET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+‘ ‘+@showColumn+‘ FROM ‘+@tabName+@strOrderType END ELSE -- 其他页 BEGIN IF @strCondition !=‘‘ set @strSql=‘SELECT TOP ‘+STR(@pageSize)+‘ ‘+@showColumn+ ‘ FROM ( SELECT ROW_NUMBER() OVER (‘+@strOrderType+‘) AS RowNumber,‘ +@showColumn+‘ FROM ‘+@tabName +‘ WHERE ‘+@strCondition+‘) AS A WHERE RowNumber > ‘+ STR(@pageSize*(@currPage-1)) else set @strSql=‘SELECT TOP ‘+STR(@pageSize)+‘ ‘+@showColumn+ ‘ FROM ( SELECT ROW_NUMBER() OVER (‘+@strOrderType+‘) AS RowNumber,‘ +@showColumn+‘ FROM ‘+@tabName +‘ ‘+‘) AS A WHERE RowNumber > ‘+STR(@pageSize*(@currPage-1)) END EXEC (@strSql) END -- 存储过程结束 ------------------------------------------------
标签:exec 字段名 过程 color pre over procedure div number
原文地址:http://www.cnblogs.com/wsn1203/p/7251625.html