zoukankan      html  css  js  c++  java
  • SQLServer存储过程实现单条件分页

    SQLServer-代码:

     1 SQLServer Procedure Pagination_basic:
     2 ALTER PROCEDURE [qiancheng].[Pagination_basic] (
     3 @Table_name VARCHAR (255),
     4 --name of table
     5 @Rows_target VARCHAR (1000) = '*',
     6 --search rows 
     7 @Rows_condition VARCHAR (1000) = '',
     8 --the condition to find target (no where)
     9 @Rows_order VARCHAR (255) = '',
    10 --the rows to rank
    11 @Order_type INT = 0,
    12 -- *Q*C* 0 normal 1 down
    13 @PageSizes INT = 10,
    14 --the size of each page
    15 @PageIndex INT = 1,
    16 --current page
    17 @ShowPages INT,
    18 --whether show the pages *Q*C* 1-yes 0-no
    19 @ShowRecords INT,
    20 --whether show the record *Q*C* 1-yes 0-no
    21 @Records_total INT OUTPUT,
    22 --returned total records
    23 @Pages_total INT OUTPUT --returned total pages
    24 ) AS
    25 DECLARE @MainSQL_QC nvarchar (2000) --Main SQL sentence
    26 DECLARE @Var_QC VARCHAR (100) --Temporary variate
    27 DECLARE @Order_QC VARCHAR (400) --the sort to rank
    28 SET @Records_total = 0
    29 SET @Pages_total = 0
    30 IF @ShowRecords = 1
    31 OR @ShowPages = 1
    32 BEGIN
    33 
    34 IF @Rows_condition != ''
    35 SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + '] where ' +@Rows_condition
    36 ELSE
    37 
    38 SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + ']' EXEC sp_executesql @MainSQL_QC,
    39  N'@Records_total int out' ,@Records_total OUTPUT
    40 END
    41 IF @ShowPages = 1
    42 BEGIN
    43 
    44 IF @Records_total <= @PageSizes
    45 SET @Pages_total = 1
    46 ELSE
    47 
    48 BEGIN
    49 
    50 SET @Pages_total = @Records_total /@PageSizes
    51 IF (@Records_total %@PageSizes) > 0
    52 SET @Pages_total = @Pages_total + 1
    53 END
    54 END
    55 IF @Order_type = 1
    56 BEGIN
    57 
    58 SET @Var_QC = '<(select min'
    59 SET @Order_QC = ' order by [' + @Rows_order + '] desc'
    60 END
    61 ELSE
    62 
    63 BEGIN
    64 
    65 SET @Var_QC = '>(select max'
    66 SET @Order_QC = ' order by [' + @Rows_order + '] asc'
    67 END
    68 IF @PageIndex = 1
    69 BEGIN
    70 
    71 IF @Rows_condition != ''
    72 SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where ' + @Rows_condition + ' ' + @Order_QC
    73 ELSE
    74 
    75 SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] ' + @Order_QC
    76 END
    77 ELSE
    78 
    79 BEGIN
    80 
    81 IF @Rows_condition != ''
    82 SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where [' + @Rows_order + ']' + @Var_QC + '([' + @Rows_order + ']) from (select top ' + str((@PageIndex - 1) *@PageSizes) + ' [' + @Rows_order + '] from [' + @Table_name + '] where ' + @Rows_condition + ' ' + @Order_QC + ') as Tmep_QC) and ' + @Rows_condition + ' ' + @Order_QC
    83 ELSE
    84 
    85 SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where [' + @Rows_order + ']' + @Var_QC + '([' + @Rows_order + ']) from (select top ' + str((@PageIndex - 1) *@PageSizes) + ' [' + @Rows_order + '] from [' + @Table_name + ']' + @Order_QC + ') as Tmep_QC)' + @Order_QC
    86 END EXEC (@MainSQL_QC)

    调用:execute pagination_basic 'UserDetail','*','','id','1','5','1','1','1','',''

    主要是末尾的语句,拆分下来便是这样:

    select top 每页数 列名 from [表名] where [排序字段名] <    --1 倒序输出若列 小于之前页数的最小值
    (select min ( [排序字段名] )from --2 获得一个指定列名中的最小值并输出
    (select top (当前页-1)*每页数 [排序字段名] from [表名] where [条件] [排序类型]) --3 选择之前页数总数据倒序输出
    as Tmep_QC)--4 建立一个名为Tmep_QC的临时表--2 获得一个指定列名中的最小值并输出
    and [条件] [排序类型]--1 倒序输出若列 小于之前页数的最小值
  • 相关阅读:
    学习也好,科研也罢,都有内在规律。任何事物,只消抓住规律,就等于牵住牛鼻子
    赵伟国:陆资无法进入台湾紫光要到WTO控告(芯片是为了经济安全,高通找的人不是很聪明)
    小米新旗舰“翻车” 冲击中高端凸显品控短板(小米的缺点还真不少:电商、性价比、爆款、粉丝经济,说到底也都只是商业上的创新)
    WinRarHelper帮助类
    Window7下安装Ubuntu 14.04 64bit
    Kafka基本原理
    Abot爬虫和visjs
    CLR垃圾回收的设计
    NET Core全新的配置管理
    Github Atom
  • 原文地址:https://www.cnblogs.com/mazey/p/6129866.html
Copyright © 2011-2022 走看看