USE HXXHHHDatabase
GO
/****** Object: StoredProcedure [dbo].[PagerRecord] Script Date: 07/19/2011 20:19:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[PagerRecord]
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@pagenow int=1, -- 当前页数
@pagesize int=1, -- 定义每页数目
@fldName1 varchar(255)='', -- 排序字段名
@fldName2 varchar(255)='', -- 排序字段名
@fldName3 varchar(255)='', -- 排序字段名
@OrderType1 varchar(255)='', -- 设置排序类型, Asc升序 非Asc 降序
@OrderType2 varchar(255)='', -- 设置排序类型, Asc升序 非Asc 降序
@OrderType3 varchar(255)='', -- 设置排序类型, Asc升序 非Asc 降序
@strWhere varchar(4000) = '', -- 查询条件 (注意: 不要加 where)
@recordcount int output
AS
set nocount on
declare @strSQL nvarchar(4000) -- 主语句
declare @strOrder varchar(400) -- 排序类型
declare @whereW varchar(4000)
declare @CountSQL nVarchar(4000)
set @recordcount = 0
--最多三字段排序-------------------------------------------------
if @OrderType1 <> ''
begin
set @strOrder = ' order by ' + @fldName1 +' '+@OrderType1
end
if @OrderType2 <> ''
begin
set @strOrder =@strOrder+ ' , ' + @fldName2 +' '+@OrderType2
end
if @OrderType3 <> ''
begin
set @strOrder =@strOrder+ ' , ' + @fldName3 +' '+@OrderType3
end
------------------------------------------------------------------
if @strWhere <> ''
begin
set @whereW = ' where ' + @strWhere
end
else
begin
set @whereW = ''
end
set @strSQL = 'SELECT * FROM (select ' + @strGetFields + ', ROW_NUMBER() Over(' + @strOrder + ') as rowNum from ' + @tblName + @whereW + ' ) as t where t.rowNum> ' + str((@pagenow-1)*@pagesize) + ' and t.rowNum<=' + str(@pagenow*@pagesize)
exec(@strSQL)
--select @strSQL;
set @CountSQL = 'select @recordcount=count(1) from ' + @tblName + @whereW
exec sp_executesql @CountSQL, N'@recordcount int out ',@recordcount out
set nocount off