话不多说,请看代码:

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
) AS
DECLARE @MainSQL_QC nvarchar (2000) --Main SQL sentence
DECLARE @Var_QC VARCHAR (100) --Temporary variate
DECLARE @Order_QC VARCHAR (400) --the sort to rank
SET @Records_total = 0
SET @Pages_total = 0
IF @ShowRecords = 1
OR @ShowPages = 1
BEGIN
IF @Rows_condition != ''
SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + '] where ' +@Rows_condition
ELSE
SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + ']' EXEC sp_executesql @MainSQL_QC,
 N'@Records_total int out' ,@Records_total OUTPUT
END
IF @ShowPages = 1
BEGIN
IF @Records_total <= @PageSizes
SET @Pages_total = 1
ELSE
BEGIN
SET @Pages_total = @Records_total /@PageSizes
IF (@Records_total %@PageSizes) > 0
SET @Pages_total = @Pages_total + 1
END
END
IF @Order_type = 1
BEGIN
SET @Var_QC = '<(select min'
SET @Order_QC = ' order by [' + @Rows_order + '] desc'
END
ELSE
BEGIN
SET @Var_QC = '>(select max'
SET @Order_QC = ' order by [' + @Rows_order + '] asc'
END
IF @PageIndex = 1
BEGIN
IF @Rows_condition != ''
SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where ' + @Rows_condition + ' ' + @Order_QC
ELSE
SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] ' + @Order_QC
END
ELSE
BEGIN
IF @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_QC
ELSE
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
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 倒序输出若列 小于之前页数的最小值

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!

标签:
sqlserver实现分页,存储过程实现分页,sql分页存储过程

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com

评论“SQLServer存储过程实现单条件分页”

暂无“SQLServer存储过程实现单条件分页”评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?