分页存储过程
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
USE [carsellmis_sh]
GO
/****** 对象: StoredProcedure [dbo].[UP_GetPage] 脚本日期: 07/13/2009 08:45:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: ╰⑥月の雨╮
-- Create date: 2009-07-13
-- Description: 分页
-- =============================================
ALTER PROCEDURE [dbo].[UP_GetPage]
@TableNames sysname, --要分页显示的表名
@PrimaryKey nvarchar(1000), --用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段
@Fields nvarchar(1000)='', --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段
@PageSize int=10, --每页的大小(记录数)
@CurrentPage int=1, --要显示的页码
@Filter nvarchar(1000)='', --查询条件
@Group nvarchar(1000)='', --暂时不可用(传空值即可)
@Order nvarchar(1000)='' --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC -- 用于指定排序顺序
--@PageCount int OUTPUT --总页数
AS
declare @PageCount int
declare @TotalRecord int
--检查对象是否有效
IF OBJECT_ID(@TableNames) IS NULL --OBJECT_ID 功能:返回数据库对象的标识号
BEGIN
RAISERROR(N'对象"%s"不存在',1,16,@TableNames)
RETURN
END
IF OBJECTPROPERTY(OBJECT_ID(@TableNames),N'IsTable')=0 --OBJECTPROPERTY 功能:返回当前数据库中对象的有关信息。
AND OBJECTPROPERTY(OBJECT_ID(@TableNames),N'IsView')=0
AND OBJECTPROPERTY(OBJECT_ID(@TableNames),N'IsTableFunction')=0
BEGIN
RAISERROR(N'"%s"不是表、视图或者表值函数',1,16,@TableNames)
RETURN
END
--分页字段检查
IF ISNULL(@PrimaryKey,N'')=''
BEGIN
RAISERROR(N'分页处理需要主键(或者惟一键)',1,16)
RETURN
END
--其他参数检查及规范
IF ISNULL(@CurrentPage,0) <1 SET @CurrentPage=1
IF ISNULL(@PageSize,0) <1 SET @PageSize=10
IF ISNULL(@Fields,N'')=N'' SET @Fields=N'*'
IF ISNULL(@Order,N'')=N''
SET @Order=N''
ELSE
SET @Order=N'ORDER BY '+LTRIM(@Order)
IF ISNULL(@Filter,N'')=N''
SET @Filter=N''
ELSE
SET @Filter=N'WHERE ('+@Filter+N')'
--如果@PageCount为NULL值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@PageCount赋值)
IF @PageCount IS NULL
BEGIN
DECLARE @sql nvarchar(4000)
SET @sql=N'SELECT @PageCount=COUNT(*)'
+N' FROM '+@TableNames
+N' '+@Filter
EXEC sp_executesql @sql,N'@PageCount int OUTPUT',@PageCount OUTPUT
set @TotalRecord=@PageCount
SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
END
--计算分页显示的TOPN值
DECLARE @TopN varchar(20),@TopN1 varchar(20)
SELECT @TopN=@PageSize,
@TopN1=(@CurrentPage-1)*@PageSize
--第一页直接显示
IF @CurrentPage=1
EXEC(N'SELECT TOP '+@TopN
+N' '+@Fields
+N' FROM '+@TableNames
+N' '+@Filter
+N' '+@Order)
ELSE
BEGIN
--处理别名
IF @Fields=N'*'
SET @Fields=N'a.*'
--生成主键(惟一键)处理条件
DECLARE @Filter1 nvarchar(4000),@Filter2 nvarchar(4000),
@s nvarchar(1000),@Field sysname
SELECT @Filter1=N'',@Filter2=N'',@s=@PrimaryKey
WHILE CHARINDEX(N',',@s)>0
SELECT @Field=LEFT(@s,CHARINDEX(N',',@s)-1),
@s=STUFF(@s,1,CHARINDEX(N',',@s),N''),
@Filter1=@Filter1+N' AND a.'+@Field+N'=b.'+@Field,
@Filter2=@Filter2+N' AND b.'+@Field+N' IS NULL',
@Filter=REPLACE(@Filter,@Field,N'a.'+@Field),
@Order=REPLACE(@Order,@Field,N'a.'+@Field),
@Fields=REPLACE(@Fields,@Field,N'a.'+@Field)
SELECT @Filter=REPLACE(@Filter,@s,N'a.'+@s),
@Order=REPLACE(@Order,@s,N'a.'+@s),
@Fields=REPLACE(@Fields,@s,N'a.'+@s),
@Filter1=STUFF(@Filter1+N' AND a.'+@s+N'=b.'+@s,1,5,N''),
@Filter2=CASE
WHEN @Filter='' THEN N'WHERE ('
ELSE @Filter+N' AND ('
END+N'b.'+@s+N' IS NULL'+@Filter2+N')'
--执行查询
EXEC(N'SELECT TOP '+@TopN
+N' '+@Fields
+N' FROM '+@TableNames
+N' a LEFT JOIN(SELECT TOP '+@TopN1
+N' '+@PrimaryKey
+N' FROM '+@TableNames
+N' a '+@Filter
+N' '+@Order
+N')b ON '+@Filter1
+N' '+@Filter2
+N' '+@Order)
END
--返回总记录
Select @TotalRecord AS tNum