1,
------------------------------------
--用途:支持任意排序的分页存储过程
--说明:
------------------------------------
CREATE PROCEDURE [dbo].[UP_GetRecordByPageOrder]
@tblName varchar(255),
@fldName varchar(255),
@OrderfldName varchar(255), -- 排序字段名
@StatfldName varchar(255), -- 统计字段名
@PageSize int = 10,
@PageIndex int = 1,
@IsReCount bit = 0,
@OrderType bit = 0,
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(100)
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @OrderfldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @OrderfldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @fldName + ' from ['
+ @tblName + '] where [' + @OrderfldName + ']' + @strTmp + '(['
+ @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @OrderfldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @fldName + ' from ['
+ @tblName + '] where [' + @OrderfldName + ']' + @strTmp + '(['
+ @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @OrderfldName + '] 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) + ' ' + @fldName + ' from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
if @IsReCount != 0
set @strSQL = @strSQL+' select count(1) as Total from [' + @tblName + ']'
if @strWhere!=''
set @strSQL = @strSQL+' where ' + @strWhere
exec (@strSQL)
2,
------------------------------------
--用途:分页存储过程(对有主键的表效率极高)
--说明:
------------------------------------
CREATE PROCEDURE [dbo].[UP_GetRecordByPage]
AS
declare @strSQL
declare @strTmp
declare @strOrder varchar(400)
if @OrderType != 0
begin
end
else
begin
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
if @strWhere != ''
if @PageIndex = 1
begin
end
if @IsReCount != 0
exec (@strSQL)