共计 3023 个字符,预计需要花费 8 分钟才能阅读完成。
SQLServer 中怎么利用存储过程实现单条件分页,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
SQLServer Procedure Pagination_basic:ALTER PROCEDURE [qiancheng].[Pagination_basic] (@Table_name VARCHAR (255),--name of table@Rows_target VARCHAR (1000) = * ,--search rows @Rows_condition VARCHAR (1000) = ,--the condition to find target (no where)@Rows_order VARCHAR (255) = ,--the rows to rank@Order_type INT = 0,-- *Q*C* 0 normal 1 down@PageSizes INT = 10,--the size of each page@PageIndex INT = 1,--current page@ShowPages INT,--whether show the pages *Q*C* 1-yes 0-no@ShowRecords INT,--whether show the record *Q*C* 1-yes 0-no@Records_total INT OUTPUT,--returned total records@Pages_total INT OUTPUT --returned total pages) ASDECLARE @MainSQL_QC nvarchar (2000) --Main SQL sentenceDECLARE @Var_QC VARCHAR (100) --Temporary variateDECLARE @Order_QC VARCHAR (400) --the sort to rankSET @Records_total = 0SET @Pages_total = 0IF @ShowRecords = 1OR @ShowPages = 1BEGINIF @Rows_condition != SET @MainSQL_QC = select @Records_total = count(1) from [ + @Table_name + ] where +@Rows_conditionELSESET @MainSQL_QC = select @Records_total = count(1) from [ + @Table_name + ] EXEC sp_executesql @MainSQL_QC, N @Records_total int out ,@Records_total OUTPUTENDIF @ShowPages = 1BEGINIF @Records_total = @PageSizesSET @Pages_total = 1ELSEBEGINSET @Pages_total = @Records_total /@PageSizesIF (@Records_total %@PageSizes) 0SET @Pages_total = @Pages_total + 1ENDENDIF @Order_type = 1BEGINSET @Var_QC = (select min SET @Order_QC = order by [ + @Rows_order + ] desc ENDELSEBEGINSET @Var_QC = (select max SET @Order_QC = order by [ + @Rows_order + ] asc ENDIF @PageIndex = 1BEGINIF @Rows_condition != SET @MainSQL_QC = select top + str(@PageSizes) + +@Rows_target + from [ + @Table_name + ] where + @Rows_condition + + @Order_QCELSESET @MainSQL_QC = select top + str(@PageSizes) + +@Rows_target + from [ + @Table_name + ] + @Order_QCENDELSEBEGINIF @Rows_condition != 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_QCELSESET @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_QCEND 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 倒序输出若列 小于之前页数的最小值
关于 SQLServer 中怎么利用存储过程实现单条件分页问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注丸趣 TV 行业资讯频道了解更多相关知识。
正文完