sqlserver 通用分页存储过程(转)

时间:2021-07-06 14:03:58
  1 USE [AAA_TYDC]
2 GO
3 /****** Object: StoredProcedure [dbo].[proc_DataPagination] Script Date: 11/20/2014 11:04:47 ******/
4 SET ANSI_NULLS ON
5 GO
6 SET QUOTED_IDENTIFIER ON
7 GO
8
9 /*********************************************************
10 * 作 用:数据分页
11 * 作 者:evafly920
12 * 作者博客:http://blog.csdn.net/evafly920/article/details/614813
13 * 创建日期:2003-11-23
14 * 修改日期:2014-11-23
15 * 使用说明:
16 --调用例子:
17 --1.单表/单排序
18 EXEC proc_DataPagination @TableNames='bigtable',@PrimaryKey='d_id',@Fields='d_id,d_title,d_content,d_time',@PageSize=20,@CurrentPage=1,@Filter ='',@Group='',@Order='d_id desc'
19 --2.单表/多排序
20 EXEC proc_DataPagination 'bigtable','d_id','*',20,0,'','','d_time asc,d_id desc'
21 --3.多表/单排序
22 EXEC proc_DataPagination 'bigtable left join bigtable_author on bigtable.d_id=bigtable_author.BigTable_id', 'bigtable.d_id', 'bigtable.d_id,bigtable.d_title,bigtable.d_content,bigtable.d_time,bigtable_author.d_author', 20, 0, '', '', 'bigtable.d_id asc'
23 --4.多表/多排序
24 EXEC proc_DataPagination 'bigtable left join bigtable_author on bigtable.d_id=bigtable_author.BigTable_id', 'bigtable.d_id', 'bigtable.d_id,bigtable.d_title,bigtable.d_content,bigtable.d_time,bigtable_author.d_author', 20, 0, '', '', 'bigtable.d_time asc,bigtable.d_id desc'
25
26 *********************************************************/
27 ALTER PROCEDURE [dbo].[proc_DataPagination]
28 @TableNames VARCHAR(200), --表名,可以是多个表,但不能用别名
29 @PrimaryKey VARCHAR(100), --主键,可以为空,但@Order为空时该值不能为空
30 @Fields VARCHAR(200), --要取出的字段,可以是多个表的字段,可以为空,为空表示select *
31 @PageSize INT, --每页记录数
32 @CurrentPage INT, --当前页,0表示第1页
33 @Filter VARCHAR(200) = '', --条件,可以为空,不用填 where
34 @Group VARCHAR(200) = '', --分组依据,可以为空,不用填 group by
35 @Order VARCHAR(200) = '' --排序,可以为空,为空默认按主键升序排列,不用填 order by
36 AS
37 BEGIN
38 DECLARE @SortColumn VARCHAR(200)
39 DECLARE @Operator CHAR(2)
40 DECLARE @SortTable VARCHAR(200)
41 DECLARE @SortName VARCHAR(200)
42 IF @Fields = ''
43 SET @Fields = '*'
44 IF @Filter = ''
45 SET @Filter = 'WHERE 1=1'
46 ELSE
47 SET @Filter = 'WHERE ' + @Filter
48 IF @Group <>''
49 SET @Group = 'GROUP BY ' + @Group
50
51 IF @Order <> ''
52 BEGIN
53 DECLARE @pos1 INT, @pos2 INT
54 SET @Order = REPLACE(REPLACE(@Order, ' asc', ' ASC'), ' desc', ' DESC')
55 IF CHARINDEX(' DESC', @Order) > 0
56 IF CHARINDEX(' ASC', @Order) > 0
57 BEGIN
58 IF CHARINDEX(' DESC', @Order) < CHARINDEX(' ASC', @Order)
59 SET @Operator = '<='
60 ELSE
61 SET @Operator = '>='
62 END
63 ELSE
64 SET @Operator = '<='
65 ELSE
66 SET @Operator = '>='
67 SET @SortColumn = REPLACE(REPLACE(REPLACE(@Order, ' ASC', ''), ' DESC', ''), ' ', '')
68 SET @pos1 = CHARINDEX(',', @SortColumn)
69 IF @pos1 > 0
70 SET @SortColumn = SUBSTRING(@SortColumn, 1, @pos1-1)
71 SET @pos2 = CHARINDEX('.', @SortColumn)
72 IF @pos2 > 0
73 BEGIN
74 SET @SortTable = SUBSTRING(@SortColumn, 1, @pos2-1)
75 IF @pos1 > 0
76 SET @SortName = SUBSTRING(@SortColumn, @pos2+1, @pos1-@pos2-1)
77 ELSE
78 SET @SortName = SUBSTRING(@SortColumn, @pos2+1, LEN(@SortColumn)-@pos2)
79 END
80 ELSE
81 BEGIN
82 SET @SortTable = @TableNames
83 SET @SortName = @SortColumn
84 END
85 END
86 ELSE
87 BEGIN
88 SET @SortColumn = @PrimaryKey
89 SET @SortTable = @TableNames
90 SET @SortName = @SortColumn
91 SET @Order = @SortColumn
92 SET @Operator = '>='
93 END
94
95 DECLARE @type varchar(50)
96 DECLARE @prec int
97 SELECT @type=t.name, @prec=c.prec
98 FROM sysobjects o
99 JOIN syscolumns c on o.id=c.id
100 JOIN systypes t on c.xusertype=t.xusertype
101 WHERE o.name = @SortTable AND c.name = @SortName
102 IF CHARINDEX('char', @type) > 0
103 SET @type = @type + '(' + CAST(@prec AS varchar) + ')'
104
105 DECLARE @TopRows INT
106 SET @TopRows = @PageSize * @CurrentPage + 1
107 print @TopRows
108 print @Operator
109 EXEC('
110 DECLARE @SortColumnBegin ' + @type + '
111 SET ROWCOUNT ' + @TopRows + '
112 SELECT @SortColumnBegin=' + @SortColumn + ' FROM ' + @TableNames + ' ' + @Filter + ' ' + @Group + ' ORDER BY ' + @Order + '
113 SET ROWCOUNT ' + @PageSize + '
114 SELECT ' + @Fields + ' FROM ' + @TableNames + ' ' + @Filter + ' AND ' + @SortColumn + '' + @Operator + '@SortColumnBegin ' + @Group + ' ORDER BY ' + @Order + '
115 ')
116 END
117
118 GO