set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROCEDURE [dbo].[P_GetPagedReCord] (@startIndex INT, -- 开始索引号 @endindex INT, -- 结束索引号 @tblName varchar(255), -- 表名 @fldName varchar(255), -- 显示字段名 @OrderfldName varchar(255), -- 排序字段名 @IsReCount bit = 0, -- 返回记录总数, 非0 值则返回 @OrderType bit = 0, -- 设置排序类型, 非0 值则降序 @strWhere varchar(1000) = '' -- 查询条件(注意: 不要加where) ) AS declare @strSQL varchar(6000) -- 主语句 declare @strTmp varchar(100) -- 临时变量 declare @strOrder varchar(400) -- 排序类型 if @OrderType!=0 Begin set @strOrder='Desc' End else Begin set @strOrder='Asc' End set @strSQL ='WITH orderList AS ( '+ 'SELECT ROW_NUMBER() OVER (ORDER BY '+@OrderfldName+' '+@strOrder+')AS Row, '+@fldName+' '+ 'from '+@tblName if @strWhere!='' set @strSQL = @strSQL+' where ' + @strWhere set @strSQL=@strSQL+')'+ 'SELECT '+@fldName+' '+ 'FROM orderlist '+ 'WHERE Row between '+str(@startIndex)+' and '+str(@endIndex)+'' if @IsReCount != 0 Begin set @strSQL = ' select count(1) as Total from [' + @tblName + ']' if @strWhere!='' set @strSQL = @strSQL+' where ' + @strWhere End --print(@strSQL) exec (@strSQL)