温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SQLServer中怎么利用存储过程实现单条件分页

发布时间:2021-07-26 17:00:37 来源:亿速云 阅读:166 作者:Leah 栏目:数据库

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中怎么利用存储过程实现单条件分页问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI