@OrderField varchar(5000), --排序字段(必须!支持多字段)
@sqlWhere varchar(5000) = Null,--条件语句(不用加where)
@pageSize int, --每页多少条记录
@pageIndex int = 1 , --指定当前为第几页
@TotalPage int output --返回总页数
as
begin
Begin Tran --开始事务
Declare @sql nvarchar(4000);
Declare @totalRecord int;
--计算总记录数
if (@SqlWhere='' or @sqlWhere=NULL)
set @sql = 'select @totalRecord = count(*) from ' + @TableName
else
set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere
EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
--计算总页数
select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize)
if (@SqlWhere='' or @sqlWhere=NULL)
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName
else
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' where ' + @SqlWhere
--处理页数超出范围情况
if @PageIndex<=0
Set @pageIndex = 1
if @pageIndex>@TotalPage
Set @pageIndex = @TotalPage
--处理开始点和结束点
Declare @StartRecord int
Declare @EndRecord int
set @StartRecord = (@pageIndex-1)*@PageSize + 1
set @EndRecord = @StartRecord + @pageSize - 1
--继续合成sql语句
set @Sql = @Sql + ') as ' + @TableName + ' where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
Exec(@Sql)
---------------------------------------------------
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit Tran
Return @totalRecord ---返回记录总数
End
end
我做了如下修改 :_)
Declare @SQL nvarchar(4000)
Declare @PageSize int
Declare @PageIndex int
Select @SQL = 'Select * From Org_Employee'
Select @PageSize = 3
Select @PageIndex = 1
Declare @ExecuteSQL nvarchar(4000)
Set @ExecuteSQL = '
Declare @LeftRow int
Declare @RightRow int
Declare @XTable table(ID int identity, PK int )
Declare @XTmp nvarchar(4000)
Set nocount On
Insert into @XTable (PK)
Select [ID] From (' + @SQL + ') Input_SQL
--Select * From @XTable
Set RowCount ' + Convert(varchar(10),@PageSize) + '
Select Input_SQL.*
From (' + @SQL + ') Input_SQL,
(
Select [PK]
From @XTable
Where [ID] >= ' + Convert(varchar(10),@PageSize * (@PageIndex - 1) + 1) + '
And [ID] <= ' + Convert(varchar(10),@PageSize * @PageIndex ) + '
) Index_Table
Where Input_SQL.[ID] = Index_Table.[PK]
Set nocount Off
'
Print '[ExecuteSQL:]' + @ExecuteSQL
Execute(@ExecuteSQL)
@sqlWhere varchar(5000) = Null,--条件语句(不用加where)
@pageSize int, --每页多少条记录
@pageIndex int = 1 , --指定当前为第几页
@TotalPage int output --返回总页数
as
begin
Begin Tran --开始事务
Declare @sql nvarchar(4000);
Declare @totalRecord int;
--计算总记录数
if (@SqlWhere='' or @sqlWhere=NULL)
set @sql = 'select @totalRecord = count(*) from ' + @TableName
else
set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere
EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
--计算总页数
select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize)
if (@SqlWhere='' or @sqlWhere=NULL)
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName
else
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' where ' + @SqlWhere
--处理页数超出范围情况
if @PageIndex<=0
Set @pageIndex = 1
if @pageIndex>@TotalPage
Set @pageIndex = @TotalPage
--处理开始点和结束点
Declare @StartRecord int
Declare @EndRecord int
set @StartRecord = (@pageIndex-1)*@PageSize + 1
set @EndRecord = @StartRecord + @pageSize - 1
--继续合成sql语句
set @Sql = @Sql + ') as ' + @TableName + ' where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
Exec(@Sql)
---------------------------------------------------
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit Tran
Return @totalRecord ---返回记录总数
End
end
我做了如下修改 :_)
Declare @SQL nvarchar(4000)
Declare @PageSize int
Declare @PageIndex int
Select @SQL = 'Select * From Org_Employee'
Select @PageSize = 3
Select @PageIndex = 1
Declare @ExecuteSQL nvarchar(4000)
Set @ExecuteSQL = '
Declare @LeftRow int
Declare @RightRow int
Declare @XTable table(ID int identity, PK int )
Declare @XTmp nvarchar(4000)
Set nocount On
Insert into @XTable (PK)
Select [ID] From (' + @SQL + ') Input_SQL
--Select * From @XTable
Set RowCount ' + Convert(varchar(10),@PageSize) + '
Select Input_SQL.*
From (' + @SQL + ') Input_SQL,
(
Select [PK]
From @XTable
Where [ID] >= ' + Convert(varchar(10),@PageSize * (@PageIndex - 1) + 1) + '
And [ID] <= ' + Convert(varchar(10),@PageSize * @PageIndex ) + '
) Index_Table
Where Input_SQL.[ID] = Index_Table.[PK]
Set nocount Off
'
Print '[ExecuteSQL:]' + @ExecuteSQL
Execute(@ExecuteSQL)
# re: 分页存储过程2005 2006-11-24 15:39 游客 回复 更多评论
作进一步修改如下 呵呵Declare @SQL nvarchar(4000)
Declare @PKColumnName nvarchar(100)
Declare @PKColumnType nvarchar(20)
Declare @PageSize int
Declare @PageIndex int
Select @SQL = 'Select * From Org_Employee'
Select @PKColumnName = '[ID]'
Select @PKColumnType = 'int'
Select @PageSize = 3
Select @PageIndex = 2
Declare @ExecuteSQL nvarchar(4000)
Set @ExecuteSQL = '
Declare @LeftRow int
Declare @RightRow int
Declare @XTable table(XTable_Identity_Auto_Create_ID int identity,' + @PKColumnName + ' ' + @PKColumnType + ' )
Declare @XTmp nvarchar(4000)
Set nocount On
Insert into @XTable (' + @PKColumnName + ')
Select ' + @PKColumnName + ' From (' + @SQL + ') Input_SQL
--Select * From @XTable
Set RowCount ' + Convert(varchar(10),@PageSize) + '
Select Input_SQL.*
From (' + @SQL + ') Input_SQL,
(
Select ' + @PKColumnName + '
From @XTable
Where XTable_Identity_Auto_Create_ID >= ' + Convert(varchar(10),@PageSize * (@PageIndex - 1) + 1) + '
And XTable_Identity_Auto_Create_ID <= ' + Convert(varchar(10),@PageSize * @PageIndex ) + '
) Index_Table
Where Input_SQL.' + @PKColumnName + ' = Index_Table.' + @PKColumnName + '
Set nocount Off
'
Print '[ExecuteSQL:]' + @ExecuteSQL
Execute(@ExecuteSQL)