实名认证用户熊川湘 身份证号码430811198506290914

大数据优化的分页存储过程-成品

 

大数据优化的分页存储过程

ALTER PROCEDURE [dbo].[GetPagingRecord](
@tblName nvarchar(255), -- 表名
@strGetFields nvarchar(4000) = '*', --欲选择字段列表
@fldName nvarchar(255), -- 排序的字段名
@ID nvarchar(100),--主键
@Page int, --页码
@RecsPerPage int, --每页容纳的记录数
@strWhere varchar(4000) = '', -- 查询条件 (注意: 不要加 where)
@OrderType bit = 1 -- 设置排序类型, 非 0 值则降序
)
AS
DECLARE @Str nVARCHAR(4000)--临时SQL语句
declare @Sort nvarchar(50)--临时排序变量

/*处理字段的升降序 H1_start*/
if @OrderType=1
set @Sort='desc'
else
set @Sort='asc'
/*H1_end*/

/*处理查询条件 H2_start*/
set @strwhere=replace(@strwhere,'''','''''')
set @strwhere=replace(@strwhere,'--','')
set @strwhere=replace(@strwhere,';','')
set @strwhere=' where 1=1 '+@strwhere
/*H2_end*/

SET @Str='SELECT TOP '+CAST(@RecsPerPage AS VARCHAR(20))+' '+@strGetFields+' FROM '+@tblName+' T WHERE T.'+@ID+'
NOT IN (SELECT TOP
'+CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM '+@tblName+' T9
ORDER BY
'+@fldName+' '+@Sort+')
ORDER BY
'+@fldName+' '+@Sort
PRINT @Str
EXEC sp_ExecuteSql @Str

 

posted @ 2010-05-24 17:26  浪达短信群发  阅读(211)  评论(0编辑  收藏  举报