服务器之家

服务器之家 > 正文

SQLServer存储过程实现单条件分页

时间:2020-05-18 14:39     来源/作者:Mazey

话不多说,请看代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 倒序输出若列 小于之前页数的最小值

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

原文链接:http://www.cnblogs.com/mazey/p/6535510.html

相关文章

热门资讯

歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
返回顶部