【原创】SQL分页查询存储过程

 1 -------------------------------------
 2 -----作者:张欣宇
 3 -----时间:2013-06-28
 4 -----简介:根据参数和条件分页查询
 5 -------------------------------------
 6 Create proc [dbo].[Up_PagingQueryByParameter]
 7 (
 8     ----- 表名或能查询到结果的SQL语句{SQL语句左右必须有括号例:(select * from tbl1)}
 9     @TableName varchar(max),
10     ----- 要查询的列名语句; 可空默认*
11     @ColumnName varchar(5000),
12     ----- 用来排序的列;  不可为空
13     @OrderByColumnName varchar(50),
14     ----- 排序desc(倒序5.4.3.2.1)或asc(正序1.2.3.4.5); 可空默认asc
15     @ShrtBy varchar(4),
16     ----- Where条件; 可空默认1=1
17     @Where varchar(5000),
18     ----- 每页显示数; 可空默认20
19     @PageShows int,
20     ----- 当前页数; 可空默认1
21     @CurrentPage int,
22     ----- 0为分页查询;其他为查询全部; 可空默认0
23     @IsPaging int
24 )
25 as
26 begin
27     ----- 参数检查及规范
28     if isnull(@Where,N'')=N'' set @Where = N'1=1';
29     if isnull(@ColumnName,N'')=N'' set @ColumnName = N'*';
30     if isnull(@PageShows,0)<1 set @PageShows = 20;
31     if isnull(@CurrentPage,0)<1 set @CurrentPage = 1;
32     if isnull(@ShrtBy,N'')=N'' set @ShrtBy = 'asc';
33     if isnull(@IsPaging,0)<>1 set @IsPaging = 0;
34     ----- 定义
35     -- 拼接的SQL语句
36     declare @SqlStr nvarchar(max);
37     declare @SqlWithStr nvarchar(max);
38     -- 开始条数
39     declare @StartIndex int;
40     -- 结束条数
41     declare @EndIndex int;
42     ----- 赋值
43     set @StartIndex = (@CurrentPage-1)*@PageShows+1;
44     print(@CurrentPage);
45     print(@PageShows);
46     print(@StartIndex);
47     set @EndIndex = @CurrentPage*@PageShows;
48     print(@EndIndex);
49     set @OrderByColumnName=@OrderByColumnName+' '+@ShrtBy;
50     ----- 分页查询
51      set @SqlWithStr = N'with temp as(select ROW_NUMBER() over(order by '+@OrderByColumnName+N') as RowNumber,'+@ColumnName+N' from '+@TableName+N' as tableName where '+@Where+N')';
52     if(@IsPaging = 0)
53     begin
54         set @SqlStr = @SqlWithStr + N' select '+@ColumnName+N' from temp where temp.RowNumber between '+convert(nvarchar(20),@StartIndex)+N' and '+convert(nvarchar(20),@EndIndex)+N'';
55         ---- print(@SqlStr);
56         exec(@SqlStr);
57         ----- 总数查询
58         set @SqlStr = @SqlWithStr + N' select count(*) as TotalNumber from temp';
59         ---- print(@SqlStr);
60         exec(@SqlStr);
61     end
62     else
63     begin
64         set @SqlStr = @SqlWithStr + N' select '+@ColumnName+N' from temp';
65         ---- print(@SqlStr);
66         exec(@SqlStr);
67     end
68 end

 

如果您觉得本文章对你有帮助,请点推荐并关注我吧,让更多人读到本文章,谢谢!
      作者: 欣宇
      版权声明:本人博客中未标明转载、原文地址的,其余均为原创博客,版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/zhang625161495/p/6217020.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值