代码
/* ----------------------------------------------------------------------
* Create Author:用二进制做高等数学
*
* Create DateTime:2009.11.27
*
* Copyright:
*
* Description:单表分页,返回值为两张表,第一张表为符合条件的数据列表,返回的记录
* 条数为@PageSize这么多条,第二张表只有一行一列,其值为符合条件的记录
* 数,
*
*----------------------------------------------------------------------*/
CREATE PROCEDURE UP_GetRecordByPage
@tblName varchar(255)='Test', --表名
@fldName varchar(255)='UserID', --主键名
@PageSize int = 10, --页面大小
@PageIndex int = 1, --页面索引
@IsReCount bit = 1, --是否返回记录数(1:返回,0:不返回)
@OrderType bit = 0, --0:升序,1:降序
@strWhere varchar(2000)='0=0 and 1=1' --条件
with encryption
AS
declare @strSQL varchar(6000)
declare @strTmp varchar(2000)
declare @strOrder varchar(400)
--判断是升序还是降序
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
--正常SQL语句的构造
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
--条件不为空的时,SQL的构造
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
--索引为一时,SQL的构造
if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
--是否返回记录数
if @IsReCount != 0
begin
set @strSQL = @strSQL+ ' select count(*) as Total from [' + @tblName + ']'
if @strWhere != ''
set @strSQL = @strSQL+ ' where ' + @strWhere
end
exec (@strSQL)
/* ----------------------------------------------------------------------
* Create Author:用二进制做高等数学
*
* Create DateTime:2009.11.27
*
* Copyright:
*
* Description:单表分页,返回值为两张表,第一张表为符合条件的数据列表,返回的记录
* 条数为@PageSize这么多条,第二张表只有一行一列,其值为符合条件的记录
* 数,
*
*----------------------------------------------------------------------*/
CREATE PROCEDURE UP_GetRecordByPage
@tblName varchar(255)='Test', --表名
@fldName varchar(255)='UserID', --主键名
@PageSize int = 10, --页面大小
@PageIndex int = 1, --页面索引
@IsReCount bit = 1, --是否返回记录数(1:返回,0:不返回)
@OrderType bit = 0, --0:升序,1:降序
@strWhere varchar(2000)='0=0 and 1=1' --条件
with encryption
AS
declare @strSQL varchar(6000)
declare @strTmp varchar(2000)
declare @strOrder varchar(400)
--判断是升序还是降序
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
--正常SQL语句的构造
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
--条件不为空的时,SQL的构造
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
--索引为一时,SQL的构造
if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
--是否返回记录数
if @IsReCount != 0
begin
set @strSQL = @strSQL+ ' select count(*) as Total from [' + @tblName + ']'
if @strWhere != ''
set @strSQL = @strSQL+ ' where ' + @strWhere
end
exec (@strSQL)
运行结果图如下:
数据读出来了,接下来说哈我在程序里是这么处理读出来的数据的。