两个通用存储过程!经典!

1.分页存储过程

CREATE   procedure pagination

 @str_sql           varchar(1000) = '*',     -- 执行的SQL 不含Order by 内容  
 @str_orderfield    varchar(255)='''',       -- 排序的字段名 
 @page_size         int = 10,                     -- 页大小 
 @page_index        int = 0,                      -- 页码
 @order_type        int,                           -- 设置排序类型, 非 -1 值则降序 
 @total_count       int   output                 -- 返回记录总数, 非 0 值则返回 
as

---------------------
-- 获取指定页的数据--
---------------------


declare @strsql   varchar(5000)              -- 主语句
declare @strtmp   varchar(5000)             -- 临时变量
declare @strorder varchar(400)              -- 排序字串
declare @cruRow   int                            -- 当前行号
 

--执行总数统计
exec getRowCount @str_sql,@total_count output


set @strtmp =  ' select * from ' +
        '      (select top ' + convert(varchar(10),@page_size) + ' * from ' +
        '         (select top ' + convert(varchar(10),(@page_index + 1) * @page_size)  +' * from '+        -- N+1页
        '            ('+ @str_sql +') Src '

--排序方向
if @order_type !=0
 begin
 set @strsql= @strtmp +
       '          order by @str_orderfield asc) a ' +
       '       order by @str_orderfield desc)b' +
              ' order by @str_orderfield asc'
 end
else
 begin
 set @strsql= @strtmp +
       '          order by @str_orderfield desc) a ' +
       '       order by  @str_orderfieldasc)b' +
              ' order by  @str_orderfield desc'
 end

exec (@strsql)

GO

----------------------------------------------------------------------------

 

2.分页存储过程执行中用到的行数统计

create  procedure getRowCount
       @sql    nvarchar(2000),
       @count  int output
as
begin

--------------------
-- 获取数据总行数 --
--------------------

  declare @tmpsql nvarchar(2000)
  set @tmpsql='select @count=count(*)  from ('+ @sql +') a'

  execute sp_executesql @tmpsql,N'@count int output',@count output
 
end

GO

转载于:https://www.cnblogs.com/qyz123/archive/2007/01/30/634081.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值