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 倒序输出若列 小于之前页数的最小值