网上收藏的几个邹老大的分页存储过程
2007-10-25 13:44
369 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--TOP n 实现的通用分页存储过程(邹建)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE PROC sp_PageView
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@tbname sysname, --要分页显示的表名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldKey nvarchar(1000), --用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageCurrent int=1, --要显示的页码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageSize int=10, --每页的大小(记录数)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldShow nvarchar(1000)='', --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldOrder nvarchar(1000)='', --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
用于指定排序顺序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Where nvarchar(1000)='', --查询条件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageCount int OUTPUT --总页数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AS
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET NOCOUNT ON
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--检查对象是否有效
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF OBJECT_ID(@tbname) IS NULL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RAISERROR(N'对象"%s"不存在',1,16,@tbname)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RETURN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RAISERROR(N'"%s"不是表、视图或者表值函数',1,16,@tbname)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RETURN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--分页字段检查
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@FieldKey,N'')=''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RAISERROR(N'分页处理需要主键(或者惟一键)',1,16)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RETURN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--其他参数检查及规范
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@PageSize,0)<1 SET @PageSize=10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@FieldOrder,N'')=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @FieldOrder=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @FieldOrder=N'ORDER BY '+LTRIM(@FieldOrder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@Where,N'')=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @Where=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @Where=N'WHERE ('+@Where+N')'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--如果@PageCount为NULL值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@PageCount赋值)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @PageCount IS NULL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @sql nvarchar(4000)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @sql=N'SELECT @PageCount=COUNT(*)'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@Where
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC sp_executesql @sql,N'@PageCount int OUTPUT',@PageCount OUTPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--计算分页显示的TOPN值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @TopN varchar(20),@TopN1 varchar(20)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @TopN=@PageSize,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@TopN1=(@PageCurrent-1)*@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--第一页直接显示
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @PageCurrent=1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC(N'SELECT TOP '+@TopN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldShow
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@Where
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldOrder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--处理别名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @FieldShow=N'*'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @FieldShow=N'a.*'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--生成主键(惟一键)处理条件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @Where1 nvarchar(4000),@Where2 nvarchar(4000),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@s nvarchar(1000),@Field sysname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @Where1=N'',@Where2=N'',@s=@FieldKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WHILE CHARINDEX(N',',@s)>0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @Field=LEFT(@s,CHARINDEX(N',',@s)-1),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@s=STUFF(@s,1,CHARINDEX(N',',@s),N''),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Where1=@Where1+N' AND a.'+@Field+N'=b.'+@Field,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Where2=@Where2+N' AND b.'+@Field+N' IS NULL',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Where=REPLACE(@Where,@Field,N'a.'+@Field),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldOrder=REPLACE(@FieldOrder,@Field,N'a.'+@Field),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldShow=REPLACE(@FieldShow,@Field,N'a.'+@Field)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @Where=REPLACE(@Where,@s,N'a.'+@s),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldOrder=REPLACE(@FieldOrder,@s,N'a.'+@s),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldShow=REPLACE(@FieldShow,@s,N'a.'+@s),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Where1=STUFF(@Where1+N' AND a.'+@s+N'=b.'+@s,1,5,N''),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Where2=CASE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WHEN @Where='' THEN N'WHERE ('
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE @Where+N' AND ('
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END+N'b.'+@s+N' IS NULL'+@Where2+N')'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--执行查询
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC(N'SELECT TOP '+@TopN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldShow
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' a LEFT JOIN(SELECT TOP '+@TopN1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' a '+@Where
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldOrder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N')b ON '+@Where1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@Where2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldOrder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--临时表缓存实现的通用分页存储过程(邹建)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE PROC sp_PageView
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@tbname sysname, --要分页显示的表名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldKey nvarchar(1000), --用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageCurrent int=1, --要显示的页码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageSize int=10, --每页的大小(记录数)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldShow nvarchar(1000)='', --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldOrder nvarchar(1000)='', --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC用于指定排序顺序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Where nvarchar(1000)='', --查询条件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageCount int OUTPUT --总页数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AS
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET NOCOUNT ON
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--检查对象是否有效
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF OBJECT_ID(@tbname) IS NULL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RAISERROR(N'对象"%s"不存在',1,16,@tbname)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RETURN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RAISERROR(N'"%s"不是表、视图或者表值函数',1,16,@tbname)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RETURN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--分页字段检查
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@FieldKey,N'')=''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RAISERROR(N'分页处理需要主键(或者惟一键)',1,16)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RETURN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--其他参数检查及规范
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@PageSize,0)<1 SET @PageSize=10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@FieldOrder,N'')=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @FieldOrder=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @FieldOrder=N'ORDER BY '+LTRIM(@FieldOrder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@Where,N'')=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @Where=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @Where=N'WHERE ('+@Where+N')'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--如果@PageCount为NULL值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@PageCount赋值)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @PageCount IS NULL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @sql nvarchar(4000)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @sql=N'SELECT @PageCount=COUNT(*)'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@Where
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC sp_executesql @sql,N'@PageCount int OUTPUT',@PageCount OUTPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--计算分页显示的TOPN值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @TopN varchar(20),@TopN1 varchar(20)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @TopN=@PageSize,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@TopN1=@PageCurrent*@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--第一页直接显示
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @PageCurrent=1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC(N'SELECT TOP '+@TopN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldShow
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@Where
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldOrder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--生成主键(惟一键)处理条件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @Where1 nvarchar(4000),@s nvarchar(1000)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @Where1=N'',@s=@FieldKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WHILE CHARINDEX(N',',@s)>0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @s=STUFF(@s,1,CHARINDEX(N',',@s),N''),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Where1=@Where1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' AND a.'+LEFT(@s,CHARINDEX(N',',@s)-1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N'='+LEFT(@s,CHARINDEX(N',',@s)-1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @Where1=STUFF(@Where1+N' AND a.'+@s+N'='+@s,1,5,N''),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@TopN=@TopN1-@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--执行查询
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC(N'SET ROWCOUNT '+@TopN1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' SELECT '+@FieldKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' INTO # FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@Where
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldOrder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' SET ROWCOUNT '+@TopN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' DELETE FROM #'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' SELECT '+@FieldShow
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' a WHERE EXISTS(SELECT * FROM # WHERE '+@Where1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N') '+@FieldOrder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--字符串缓存实现的通用分页存储过程(邹建)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE PROC sp_PageView
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@tbname sysname, --要分页显示的表名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldKey sysname, --用于定位记录的主键(惟一键)字段,只能是单个字段
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageCurrent int=1, --要显示的页码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageSize int=10, --每页的大小(记录数)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldShow nvarchar(1000)='', --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@FieldOrder nvarchar(1000)='', --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
用于指定排序顺序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@Where nvarchar(1000)='', --查询条件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageCount int OUTPUT --总页数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AS
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @sql nvarchar(4000)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET NOCOUNT ON
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--检查对象是否有效
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF OBJECT_ID(@tbname) IS NULL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RAISERROR(N'对象"%s"不存在',1,16,@tbname)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RETURN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RAISERROR(N'"%s"不是表、视图或者表值函数',1,16,@tbname)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RETURN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--分页字段检查
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@FieldKey,N'')=''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RAISERROR(N'分页处理需要主键(或者惟一键)',1,16)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
RETURN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--其他参数检查及规范
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@PageSize,0)<1 SET @PageSize=10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@FieldOrder,N'')=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @FieldOrder=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @FieldOrder=N'ORDER BY '+LTRIM(@FieldOrder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@Where,N'')=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @Where=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @Where=N'WHERE ('+@Where+N')'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--如果@PageCount为NULL值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@PageCount赋值)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @PageCount IS NULL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @sql=N'SELECT @PageCount=COUNT(*)'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@Where
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC sp_executesql @sql,N'@PageCount int OUTPUT',@PageCount OUTPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--计算分页显示的TOPN值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @TopN varchar(20),@TopN1 varchar(20)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @TopN=@PageSize,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@TopN1=@PageCurrent*@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--第一页直接显示
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @PageCurrent=1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC(N'SELECT TOP '+@TopN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldShow
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@Where
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldOrder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @PageCurrent=@TopN1,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@sql=N'SELECT @n=@n-1,@s=CASE WHEN @n<'+@TopN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' THEN @s+N'',''+QUOTENAME(RTRIM(CAST('+@FieldKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' as varchar(8000))),N'''''''') ELSE N'''' END FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@Where
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldOrder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET ROWCOUNT @PageCurrent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC sp_executesql @sql,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
N'@n int,@s nvarchar(4000) OUTPUT',
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageCurrent,@sql OUTPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET ROWCOUNT 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @sql=N''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC(N'SELECT TOP 0'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldShow
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @sql=STUFF(@sql,1,1,N'')
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--执行查询
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC(N'SELECT TOP '+@TopN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' '+@FieldShow
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' FROM '+@tbname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' WHERE '+@FieldKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N' IN('+@sql
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
+N') '+@FieldOrder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--使用系统存储过程实现的通用分页存储过程(邹建)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE PROC sp_PageView
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@sql ntext, --要执行的sql语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageCurrent int=1, --要显示的页码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageSize int=10, --每页的大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageCount int OUTPUT --总页数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AS
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET NOCOUNT ON
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @p1 int
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--初始化分页游标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC sp_cursoropen
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@cursor=@p1 OUTPUT,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@stmt=@sql,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@scrollopt=1,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@ccopt=1,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@rowcount=@PageCount OUTPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--计算总页数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@PageSize,0)<1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @PageSize=10
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@PageCurrent,0)<1 OR ISNULL(@PageCurrent,0)>@PageCount
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @PageCurrent=1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @PageCurrent=(@PageCurrent-1)*@PageSize+1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--显示指定页的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC sp_cursorfetch @p1,16,@PageCurrent,@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--关闭分页游标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC sp_cursorclose @p1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--根据分类表实现的分页存储过程(邹建)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--要分页的原始数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE tb(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ID int PRIMARY KEY, --记录编号
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
grade varchar(10), --类别名称
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
uptime datetime) --更新时间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
INSERT tb SELECT 1 ,'a','2004-12-11'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 2 ,'b','2004-12-11'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 3 ,'c','2004-12-11'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 4 ,'a','2004-12-12'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 5 ,'c','2004-12-13'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 6 ,'c','2004-12-13'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 7 ,'a','2004-12-14'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 8 ,'a','2004-12-15'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 9 ,'b','2004-12-16'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 10,'b','2004-12-17'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 11,'a','2004-12-17'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--分页定义表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE tb_Page(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
grade varchar(10) PRIMARY KEY, --类别名称,与tb表的grade关联
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Records int, --每页显示的记录数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Orders int) --在页中的显示顺序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
INSERT tb_Page SELECT 'c',2,1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 'b',1,2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UNION ALL SELECT 'a',2,3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
GO
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--实现分页处理的存储过程
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE PROC p_PageView
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PageCurrent int=1 --要显示的当前页码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AS
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET NOCOUNT ON
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--得到每页的记录数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @PageSize int
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @PageSize=SUM(Records) FROM tb_Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF ISNULL(@PageSize,0)<0 RETURN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--分页显示处理
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @PageCurrent=@PageCurrent*@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET ROWCOUNT @PageCurrent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT SID=IDENTITY(int,1,1),ID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
INTO # FROM(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT TOP 100 PERCENT a.ID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FROM tb a
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
LEFT JOIN tb_Page b ON a.grade=b.grade
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ORDER BY CASE WHEN b.grade IS NULL THEN 1 ELSE 0 END,--分类没有定义的显示在最后
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
((SELECT COUNT(*) FROM tb
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WHERE grade=a.grade
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AND (uptime>a.uptime OR uptime=a.uptime AND id>=a.id))-1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/b.Records,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
b.Orders,a.ID DESC)a
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @PageCurrent>@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @PageCurrent=@PageCurrent-@PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET ROWCOUNT @PageCurrent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DELETE FROM #
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT a.* FROM tb a,# b
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
WHERE a.ID=b.ID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ORDER BY b.SID
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
GO
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--调用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC p_PageView 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*--结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ID grade uptime
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
----------- ---------- ------------------------------------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
3 c 2004-12-11 00:00:00.000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
9 b 2004-12-16 00:00:00.000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
7 a 2004-12-14 00:00:00.000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
4 a 2004-12-12 00:00:00.000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
2 b 2004-12-11 00:00:00.000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
--*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
还有一个,论坛里找的,不是老大的:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE PROCEDURE SP_Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@TB VARCHAR(50),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@COL VARCHAR(50),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-- @COLTYPE INT,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@ORDERBY BIT,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@COLLIST VARCHAR(800),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PAGESIZE INT,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PAGE INT,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@CONDITION VARCHAR(800),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@RecPages INT,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@RecCount INT OUTPUT,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@PAGES INT OUTPUT,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@OUTSQL NVARCHAR(4000) OUTPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AS
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @SQL NVARCHAR(4000)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @WHERE1 VARCHAR(800)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DECLARE @WHERE2 VARCHAR(800)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @CONDITION IS NULL OR RTRIM(@CONDITION) = ''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @WHERE1=' WHERE '
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @WHERE2=' '
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BEGIN
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @WHERE1=' WHERE ('+@CONDITION+') AND '
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @WHERE2=' WHERE ('+@CONDITION+') '
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @SQL = 'SELECT @RecCount=COUNT(*),@PAGES=CEILING((+0.0)/'+CAST(@PAGESIZE AS VARCHAR)+') FROM '+@TB+@WHERE2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @RecPages = 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC SP_EXECUTESQL @SQL,N'@PAGES INT OUTPUT,@RecCount INT OUTPUT',@PAGES OUTPUT,@RecCount OUTPUT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SELECT @PAGES = @RecPages
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @ORDERBY=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @SQL= 'SELECT TOP '+CAST(@PAGESIZE AS VARCHAR)+' '+@COLLIST+' FROM '+@TB+@WHERE1+@COL+'>(SELECT MAX('+@COL+') '+' FROM (SELECT TOP '+CAST(@PAGESIZE*(@PAGE-1) AS VARCHAR)+' '+
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@COL+' FROM '+@TB+@WHERE2+'ORDER BY '+@COL+') t) ORDER BY '+@COL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ELSE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @SQL='SELECT TOP '+CAST(@PAGESIZE AS VARCHAR)+' '+@COLLIST+' FROM '+@TB+@WHERE1+@COL+'<(SELECT MIN('+@COL+') '+' FROM (SELECT TOP '+CAST(@PAGESIZE*(@PAGE-1) AS VARCHAR)+' '+
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@COL+' FROM '+@TB+@WHERE2+'ORDER BY '+@COL+' DESC) t) ORDER BY '+@COL+' DESC'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
IF @PAGE=1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @SQL='SELECT TOP '+CAST(@PAGESIZE AS VARCHAR)+' '+@COLLIST+' FROM '+@TB+@WHERE2+'ORDER BY '+@COL+CASE @ORDERBY WHEN 0 THEN '' ELSE ' DESC' END
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
SET @OUTSQL = @SQL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EXEC(@SQL)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
GO
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 网上收藏的几个邹老大的分页存储过程
- 网上收藏的几个邹老大的分页存储过程
- SQL 存储过程分页--在网上找到代码,收藏起来
- 收藏邹建大哥的存储过程通用分页
- 邹老大的分页sql存储过程
- Discuz!NT 代码阅读笔记(8)--DNT的几个分页存储过程解析
- Discuz!NT 代码阅读笔记(8)--DNT的几个分页存储过程解析
- 自已写的几个分页的存储过程
- Discuz!NT 代码阅读笔记(8.1)--DNT的几个分页存储过程解析
- 千万数量级分页存储过程【深海收藏】
- [SQL]对于分页存储过程,李洪根说...(收藏)
- 最热网友收藏:利用SqlServer内部存储过程实现快速方便的分页(2007年第7周)
- 几个收藏的根据数据库生成Insert语句的存储过程
- 几个收藏的根据数据库生成Insert语句的存储过程[修正版]
- asp.net利用存储过程分页代码收藏
- 在网上找到的sql2000/2005/oracle分页控件存储过程,记下以备后用
- 项目中的几个常用的分页存储过程
- 通用分页显示查询存储过程(SQL Server)
- 存储过程分页(3)
- ASP.NET结合存储过程写的通用搜索分页程序