复制代码 代码如下:
—————————————————-
–针对SQL 2000 的分页存储过程
–Time:2008-9-25
—————————————————-
ALTER PROCEDURE [dbo].[uoSp_RecordPager]
–要分页显示的表名,可多表连查,但不能使用别名。
–例:uo_Article LEFT JOIN uo_ArticleClass ON uo_Article.AClassID=uo_ArticleClass.ID
@Table_info varchar(100),
–用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段
@field_id nvarchar(1000),
–要显示的页码
@CurrentPage int=1,
–每页的大小(记录数)
@PageSize int=10,
–以逗号分隔的要显示的字段列表,如果不指定,则为*。但多表联查时,若有同名字段,则必须显示指定要查的字段。
–例:uo_Article.*,uo_ArticleClass.ClassName
@Field_info nvarchar(1000),
–以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC 用于指定排序顺序
@Field_Order nvarchar(1000)= ‘ ‘,
–查询条件
@otherwhere nvarchar(1000)= ‘ ‘,
@RecordCount int OUTPUT, –总行数
@PageCount int OUTPUT, –总页数
@SQLSTR nvarchar(2000) output
AS
SET NOCOUNT ON
–分页字段检查
IF ISNULL(@field_id,N’ ‘)= ‘ ‘
BEGIN
RAISERROR(N’分页处理需要主键(或者惟一键) ‘,1,16)
RETURN
END
–其他参数检查及规范
IF ISNULL(@PageSize,0) <1 SET @PageSize=10
IF ISNULL(@Field_info,N’ ‘)=N’ ‘ SET @Field_info=N’* ‘
IF ISNULL(@Field_Order,N’ ‘)=N’ ‘
SET @Field_Order=N’ ‘
ELSE
SET @Field_Order=N’ORDER BY ‘+LTRIM(@Field_Order)
IF ISNULL(@otherwhere,N’ ‘)=N’ ‘
SET @otherwhere=N’ ‘
ELSE
SET @otherwhere=N’ WHERE (‘+@otherwhere+N’) ‘
–计算@RecordCount
declare @sqlt nvarchar(1000)
set @sqlt = ‘SELECT @RecordCount = COUNT(‘ + @Field_id + ‘) FROM ‘ + @Table_Info + @otherwhere
exec sp_executesql @sqlt,N’@RecordCount int output’,@RecordCount output
–如果@PageCount为NULL值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@PageCount赋值)
IF @PageCount IS NULL
BEGIN
DECLARE @sql nvarchar(4000)
SET @sql=N’SELECT @PageCount=COUNT(*) ‘
+N’ FROM ‘+@Table_info
+N’ ‘+@otherwhere
EXEC sp_executesql @sql,N’@PageCount int OUTPUT ‘,@PageCount OUTPUT
SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
END
IF ISNULL(@CurrentPage,0) <1 SET @CurrentPage=1
IF @CurrentPage > @PageCount and @PageCount>0 SET @CurrentPage=@PageCount
–第一页直接显示
IF @CurrentPage = 1
BEGIN
SET @SQLSTR = N’SELECT TOP ‘ + str(@PageSize) +N’ ‘+@Field_info +N’ FROM ‘+@Table_info +N’ ‘+@otherwhere +N’ ‘+@Field_Order
–PRINT @SQLSTR
EXEC(@SQLSTR)
END
ELSE
BEGIN
—————————————————-
–获取第一个表的表名
DECLARE @FirstTableName varchar(20)
SET @FirstTableName = @Table_info
IF CHARINDEX(N’,’,@FirstTableName)>0 OR CHARINDEX(N’.’,@FirstTableName)> 0 OR CHARINDEX(N’ ‘,@FirstTableName)> 0
BEGIN
WHILE CHARINDEX(N’,’,@FirstTableName)> 0
SELECT @FirstTableName=LEFT(@FirstTableName,CHARINDEX(N’,’,@FirstTableName)-1)
WHILE CHARINDEX(N’.’,@FirstTableName)> 0
SELECT @FirstTableName=LEFT(@FirstTableName,CHARINDEX(N’.’,@FirstTableName)-1)
WHILE CHARINDEX(N’ ‘,@FirstTableName)> 0
SELECT @FirstTableName=LEFT(@FirstTableName,CHARINDEX(N’ ‘,@FirstTableName)-1)
END
–构造SQL语句
SET @SQLSTR = N’SELECT * FROM (‘
+ N’ SELECT TOP ‘+STR(@PageSize*@CurrentPage) +N’ ‘+ @Field_info + N’ FROM ‘+@Table_info +N’ ‘+@otherwhere +N’ ‘+@Field_Order
+ N’) ‘+@FirstTableName+N’ WHERE ‘+@field_id+N’ NOT IN (‘
+ N’ SELECT TOP ‘+STR(@PageSize*(@CurrentPage-1))+N’ ‘+ @field_id + N’ FROM ‘+@Table_info +N’ ‘+@otherwhere +N’ ‘+@Field_Order
+ N’) ‘
+ N’ ‘+@Field_Order
EXEC(@SQLSTR)
—————————————————-
END