经典的翻页存储过程[推荐]


最近找到一很好的SQL存储过程,用过都觉得很不错,执行效率也可以,在此推荐给大家.
                                                                                                                                                            ——缘易

None.gif CREATE   proc  up_GetTopicList 
None.gif
@a_TableList   Varchar ( 200 ), --要查询的字段
None.gif
@a_TableName   Varchar ( 30 ), --要查询的表名
None.gif
@a_SelectWhere   Varchar ( 500 ), --查询限制条件
None.gif
@a_SelectOrderId   Varchar ( 20 ), --查询主键
None.gif
@a_SelectOrder   Varchar ( 50 ), --排序字段
None.gif
@a_intPageNo   int
None.gif
@a_intPageSize   int
None.gif
@RecordCount   int  OUTPUT 
None.gif
as  
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*定义局部变量*/  
None.gif
declare   @intBeginID   int  
None.gif
declare   @intEndID   int  
None.gif
declare   @intRootRecordCount   int  
None.gif
declare   @intRowCount   int  
None.gif
declare   @TmpSelect   NVarchar ( 600
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*关闭计数*/  
None.gif
set  nocount  on  
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*求总共根贴数*/  
None.gif
None.gif
select   @TmpSelect   =   ' set nocount on;select @SPintRootRecordCount = count(*) from  ' + @a_TableName + '   ' + @a_SelectWhere  
None.gif
execute  sp_executesql 
None.gif
@TmpSelect
None.gifN
' @SPintRootRecordCount int OUTPUT '
None.gif
@SPintRootRecordCount = @intRootRecordCount  OUTPUT 
None.gif
None.gif
select   @RecordCount   =   @intRootRecordCount  
None.gif
None.gif
if  ( @intRootRecordCount   =   0 -- 如果没有贴子,则返回零 
None.gif
return   0  
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*判断页数是否正确*/  
None.gif
if  ( @a_intPageNo   -   1 *   @a_intPageSize   >   @intRootRecordCount  
None.gif
return  ( - 1
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*求开始rootID*/  
None.gif
set   @intRowCount   =  ( @a_intPageNo   -   1 *   @a_intPageSize   +   1  
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*限制条数*/  
None.gif
None.gif
select   @TmpSelect   =   ' set nocount on;set rowcount @SPintRowCount;select @SPintBeginID =  ' + @a_SelectOrderId + '  from  ' + @a_TableName + '   ' + @a_SelectWhere + '   ' + @a_SelectOrder  
None.gif
execute  sp_executesql 
None.gif
@TmpSelect
None.gifN
' @SPintRowCount int,@SPintBeginID int OUTPUT '
None.gif
@SPintRowCount = @intRowCount , @SPintBeginID = @intBeginID  OUTPUT 
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*结束rootID*/  
None.gif
set   @intRowCount   =   @a_intPageNo   *   @a_intPageSize  
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*限制条数*/  
None.gif
None.gif
select   @TmpSelect   =   ' set nocount on;set rowcount @SPintRowCount;select @SPintEndID =  ' + @a_SelectOrderId + '  from  ' + @a_TableName + '   ' + @a_SelectWhere + '   ' + @a_SelectOrder  
None.gif
execute  sp_executesql 
None.gif
@TmpSelect
None.gifN
' @SPintRowCount int,@SPintEndID int OUTPUT '
None.gif
@SPintRowCount = @intRowCount , @SPintEndID = @intEndID  OUTPUT 
None.gif
None.gif
None.gif
if   @a_SelectWhere = ''   or   @a_SelectWhere   IS   NULL  
None.gif
select   @TmpSelect   =   ' set nocount off;set rowcount 0;select  ' + @a_TableList + '  from  ' + @a_TableName + '  where  ' + @a_SelectOrderId + '  between  '  
None.gif
else  
None.gif
select   @TmpSelect   =   ' set nocount off;set rowcount 0;select  ' + @a_TableList + '  from  ' + @a_TableName + '   ' + @a_SelectWhere + '  and  ' + @a_SelectOrderId + '  between  '  
None.gif
None.gif
if   @intEndID   >   @intBeginID  
None.gif
select   @TmpSelect   =   @TmpSelect + ' @SPintBeginID and @SPintEndID ' + '   ' + @a_SelectOrder  
None.gif
else  
None.gif
select   @TmpSelect   =   @TmpSelect + ' @SPintEndID and @SPintBeginID ' + '   ' + @a_SelectOrder  
None.gif
None.gif
execute  sp_executesql 
None.gif
@TmpSelect
None.gifN
' @SPintEndID int,@SPintBeginID int '
None.gif
@SPintEndID = @intEndID , @SPintBeginID = @intBeginID  
None.gif
None.gif
return ( @@rowcount
None.gif
-- select @@rowcount 
None.gif

None.gif
GO
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值