通用分页存储过程

这是一个SQL存储过程,用于实现通用的分页查询功能。根据输入的表名、主键、查询条件、排序字段、页码和页大小,该存储过程能够动态构建并执行SQL,以获取所需页面的数据。同时,它还提供了是否统计总数的选项。
摘要由CSDN通过智能技术生成

set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[pagination]

@TableName varchar(255), -- 表名

@PrimaryKey varchar(255),--主键或者唯一约束字段

@DoCount int = 2, -- 需不需要进行总数统计.2是统计条数及查询 1是只统计总条数,不进行查询  0进行分页查询,不进行统计

@StrWhere varchar(1500) = '', -- 查询条件 (注意: 不要加 where)

@OrderField varchar(255)=@PrimaryKey, -- 排序的字段名,默认按照主键排序

@PageIndex int = 1, -- 页码

@PageSize int = 10, -- 页尺寸

@OrderType bit = 0 ,-- 设置排序类型, 非 0 值则降序

@StrGetFields varchar(1000) = '*'-- 需要返回的列

AS

declare @strSQL varchar(5000) -- 主语句

declare @strTmp varchar(110) -- 临时变量

declare @strOrder varchar(400) -- 排序类型

/********************************************

@DoCount传递过来的不是0,就执行总数统计

********************************************/

if @DoCount = 1

begin

if @StrWhere!=''

set @strSQL = 'select count(*) as Total from ' + @TableName + ' where '+@strWhere

else

set @strSQL = 'select count(*) as Total from ' + @TableName

 

execute(@strSQL)

end

/********************************************

以下的所有代码都是@DoCount为0的情况:

********************************************/

if @DoCount=0

begin

/********************************************

确定是升序还是降序

********************************************/

if @OrderType != 0

begin

set @strTmp = '<(select min'

set @strOrder = 'order by ' + @OrderField +' desc'

end

else

begin

set @strTmp = '>(select max'

set @strOrder = 'order by ' + @OrderField +' asc'

end

 

/**********************************************

为了加快执行速度,判断一下是不是第一页

***********************************************/

if @PageIndex = 1

begin

if @strWhere != ''

set @strSQL ='select top ' + str(@PageSize) +' '+@StrGetFields+ ' from ' + @TableName + ' where ' + @strWhere + ' ' + @strOrder

else

set @strSQL ='select top ' + str(@PageSize) +' '+@strGetFields+ ' from '+ @TableName + ' '+ @strOrder

end

/**********************************************

不是第一页

***********************************************/

else

 

begin

if @strWhere=''

set @strSQL='select top '+str(@PageSize)+' '+@strGetFields+' from '+ @TableName +' where '+@PrimaryKey+@strTmp+'('+@PrimaryKey+') as '+@PrimaryKey+' from (select top '+str((@PageIndex-1)*@PageSize)+' '+@PrimaryKey+' from '+@TableName+' '+@strOrder+') as T) '+@strOrder

else

set @strSQL='select top '+str(@PageSize)+' '+@strGetFields+' from '+ @TableName +' where '+@PrimaryKey+@strTmp+'('+@PrimaryKey+') as '+@PrimaryKey+' from (select top '+str((@PageIndex-1)*@PageSize)+' '+@PrimaryKey+' from '+@TableName+' where '+@StrWhere+' '+@strOrder+') as T) and '+@StrWhere+' '+@strOrder

end

--print @strSQL --打印sql语句

execute(@strSQL)

end

 

if @DoCount=2

begin

--记录数

if @StrWhere!=''

set @strSQL = 'select count(*) as Total from ' + @TableName + ' where '+@strWhere

else

set @strSQL = 'select count(*) as Total from ' + @TableName

 

execute(@strSQL)

--查询

if @OrderType != 0

begin

set @strTmp = '<(select min'

set @strOrder = 'order by ' + @OrderField +' desc'

end

else

begin

set @strTmp = '>(select max'

set @strOrder = 'order by ' + @OrderField +' asc'

end

 

/**********************************************

为了加快执行速度,判断一下是不是第一页

***********************************************/

if @PageIndex = 1

begin

if @strWhere != ''

set @strSQL ='select top ' + str(@PageSize) +' '+@StrGetFields+ ' from ' + @TableName + ' where ' + @strWhere + ' ' + @strOrder

else

set @strSQL ='select top ' + str(@PageSize) +' '+@strGetFields+ ' from '+ @TableName + ' '+ @strOrder

end

/**********************************************

不是第一页

***********************************************/

else

begin

if @strWhere=''

set @strSQL='select top '+str(@PageSize)+' '+@strGetFields+' from '+ @TableName +' where '+@PrimaryKey+@strTmp+'('+@PrimaryKey+') as '+@PrimaryKey+' from (select top '+str((@PageIndex-1)*@PageSize)+' '+@PrimaryKey+' from '+@TableName+' '+@strOrder+') as T) '+@strOrder

else

set @strSQL='select top '+str(@PageSize)+' '+@strGetFields+' from '+ @TableName +' where '+@PrimaryKey+@strTmp+'('+@PrimaryKey+') as '+@PrimaryKey+' from (select top '+str((@PageIndex-1)*@PageSize)+' '+@PrimaryKey+' from '+@TableName+' where '+@StrWhere+' '+@strOrder+') as T) and '+@StrWhere+' '+@strOrder

end

--print @strSQL --打印sql语句

execute(@strSQL)

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值