Code
1 CREATE PROC [dbo].[P_viewPage_A]
2 /*
3 nzperfect [no_mIss] 高效通用分页存储过程(双向检索) 2009.4.22
4 敬告:适用于单一主键或存在唯一值列的表或视图
5 ps:Sql语句为8000字节,调用时请注意传入参数及sql总长度不要超过指定范围
6 */
7 @TableName VARCHAR(200), --表名
8 @FieldList VARCHAR(2000), --显示列名,如果是全部字段则为*
9 @PrimaryKey VARCHAR(100), --单一主键或唯一值键
10 @Where VARCHAR(2000), --查询条件 不含'where'字符,如id>10 and len(userid)>9
11 @Order VARCHAR(1000), --排序(多列排序情况) 不含'order by'字符,如id asc,userid desc,必须指定asc或desc
12 --注意当@SortType=3时生效,记住一定要在最后加上主键,否则会让你比较郁闷
13 @SortType INT, --排序规则 1:正序asc 2:倒序desc 3:多列排序方法
14 @RecorderCount INT, --记录总数 0:会返回总记录
15 @PageSize INT, --每页输出的记录数
16 @PageIndex INT, --当前页数
17 @TotalCount INT OUTPUT, --记返回总记录
18 @TotalPageCount INT OUTPUT --返回总页数
19 AS
20 SET NOCOUNT ON
21 IF ISNULL(@TotalCount,'') = '' SET @TotalCount = 0
22 SET @Order = RTRIM(LTRIM(@Order))
23 SET @PrimaryKey = RTRIM(LTRIM(@PrimaryKey))
24 SET @FieldList = REPLACE(RTRIM(LTRIM(@FieldList)),' ','')
25 WHILE CHARINDEX(', ',@Order) > 0 OR CHARINDEX(' ,',@Order) > 0
26 BEGIN
27 SET @Order = REPLACE(@Order,', ',',')
28 SET @Order = REPLACE(@Order,' ,',',')
29 END
30 IF ISNULL(@TableName,'') = '' OR ISNULL(@FieldList,'') = ''
31 OR ISNULL(@PrimaryKey,'') = ''
32 OR @SortType < 1 OR @SortType >3
33 OR @RecorderCount < 0 OR @PageSize < 0 OR @PageIndex < 0
34 BEGIN
35 PRINT('ERR_00')
36 RETURN
37 END
38 IF @SortType = 3
39 BEGIN
40 IF (UPPER(RIGHT(@Order,4))!=' ASC' AND UPPER(RIGHT(@Order,5))!=' DESC')
41 BEGIN PRINT('ERR_02') RETURN END
42 END
43 DECLARE @new_where1 VARCHAR(1000)
44 DECLARE @new_where2 VARCHAR(1000)
45 DECLARE @new_order1 VARCHAR(1000)
46 DECLARE @new_order2 VARCHAR(1000)
47 DECLARE @new_order3 VARCHAR(1000)
48 DECLARE @temptable VARCHAR(1000)
49 DECLARE @Sql VARCHAR(8000)
50 DECLARE @SqlCount NVARCHAR(4000)
51 SET @temptable=@TableName
52 IF ISNULL(@where,'') = ''
53 BEGIN
54 SET @new_where1 = ' '
55 SET @new_where2 = ' WHERE '
56 END
57 ELSE
58 BEGIN
59 SET @new_where1 = ' WHERE ' + @where
60 SET @new_where2 = ' WHERE ' + @where + ' AND '
61 END
62 IF ISNULL(@order,'') = '' OR @SortType = 1 OR @SortType = 2
63 BEGIN
64 IF @SortType = 1
65 BEGIN
66 SET @new_order1 = ' ORDER BY ' + @PrimaryKey + ' ASC'
67 SET @new_order2 = ' ORDER BY ' + @PrimaryKey + ' DESC'
68 END
69 IF @SortType = 2
70 BEGIN
71 SET @new_order1 = ' ORDER BY ' + @PrimaryKey + ' DESC'
72 SET @new_order2 = ' ORDER BY ' + @PrimaryKey + ' ASC'
73 END
74 END
75 ELSE
76 BEGIN
77 SET @new_order1 = ' ORDER BY ' + @Order
78 END
79 IF @SortType = 3 AND CHARINDEX(','+@PrimaryKey+' ',','+@Order)>0
80 BEGIN
81 SET @new_order1 = ' ORDER BY ' + @Order
82 SET @new_order2 = @Order + ','
83 SET @new_order2 = REPLACE(REPLACE(@new_order2,'ASC,','{ASC},'),'DESC,','{DESC},')
84 SET @new_order2 = REPLACE(REPLACE(@new_order2,'{ASC},','DESC,'),'{DESC},','ASC,')
85 SET @new_order2 = ' ORDER BY ' + SUBSTRING(@new_order2,1,LEN(@new_order2)-1)
86 IF @FieldList <> '*'
87 BEGIN
88 SET @new_order3 = REPLACE(REPLACE(@Order + ',','ASC,',','),'DESC,',',')
89 SET @FieldList = ',' + @FieldList
90 WHILE CHARINDEX(',',@new_order3)>0
91 BEGIN
92 IF CHARINDEX(SUBSTRING(','+@new_order3,1,CHARINDEX(',',@new_order3)),','+@FieldList+',')>0
93 BEGIN
94 SET @FieldList =
95 @FieldList + ',' + SUBSTRING(@new_order3,1,CHARINDEX(',',@new_order3))
96 END
97 SET @new_order3 =
98 SUBSTRING(@new_order3,CHARINDEX(',',@new_order3)+1,LEN(@new_order3))
99 END
100 SET @FieldList = SUBSTRING(@FieldList,2,LEN(@FieldList))
101 END
102 END
103 --根据所要取的记录数@RecorderCount取出总记录数及总页数
104 SET @SqlCount = 'SELECT @TotalCount=COUNT(*),@TotalPageCount=CEILING((COUNT(*)+0.0)/'
105 + CAST(@PageSize AS VARCHAR)+') FROM ' + @temptable + @new_where1
106 IF @RecorderCount = 0
107 BEGIN
108 EXEC SP_EXECUTESQL @SqlCount,N'@TotalCount INT OUTPUT,@TotalPageCount INT OUTPUT',
109 @TotalCount OUTPUT,@TotalPageCount OUTPUT
110 END
111 ELSE
112 BEGIN
113 SELECT @TotalCount = @RecorderCount,@TotalPageCount=CEILING((@TotalCount+0.0)/@PageSize)
114 set @SqlCount='drop table temp select TOP('+CAST(@TotalCount As varchar)+') * into temp from '+@temptable+ @new_where1 + @new_order1
115 exec sp_executesql @SqlCount
116 set @temptable='temp'
117 END
118 --取数据
119 IF @PageIndex = 1 OR @PageIndex >= CEILING((@TotalCount+0.0)/@PageSize) OR @PageIndex =0
120 BEGIN
121 IF @PageIndex = 1 OR @PageIndex =0 --返回第一页数据
122 BEGIN
123 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
124 + @temptable + @new_where1 + @new_order1
125 END
126 IF @PageIndex >= CEILING((@TotalCount+0.0)/@PageSize) --返回最后一页数据
127 BEGIN
128 SET @PageIndex = CEILING((@TotalCount+0.0)/@PageSize)
129 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('
130 + 'SELECT TOP ' + STR(ABS(@PageSize*@PageIndex-@TotalCount-@PageSize))
131 + ' ' + @FieldList + ' FROM '
132 + @temptable + @new_where1 + @new_order2 + ' ) AS TMP '
133 + @new_order1
134 END
135 END
136 ELSE
137 BEGIN
138 IF @SortType = 1 --仅主键正序排序
139 BEGIN
140 IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向检索
141 BEGIN
142 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
143 + @temptable + @new_where2 + @PrimaryKey + ' > '
144 + '(SELECT MAX(' + @PrimaryKey + ') FROM (SELECT TOP '
145 + STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
146 + ' FROM ' + @temptable
147 + @new_where1 + @new_order1 +' ) AS TMP) '+ @new_order1
148 END
149 ELSE --反向检索
150 BEGIN
151 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('
152 + 'SELECT TOP ' + STR(@PageSize) + ' '
153 + @FieldList + ' FROM '
154 + @temptable + @new_where2 + @PrimaryKey + ' < '
155 + '(SELECT MIN(' + @PrimaryKey + ') FROM (SELECT TOP '
156 + STR(@TotalCount-@PageSize*@PageIndex) + ' ' + @PrimaryKey
157 + ' FROM ' + @temptable
158 + @new_where1 + @new_order2 +' ) AS TMP) '+ @new_order2
159 + ' ) AS TMP ' + @new_order1
160 END
161 END
162 IF @SortType = 2 --仅主键反序排序
163 BEGIN
164 IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向检索
165 BEGIN
166 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
167 + @temptable + @new_where2 + @PrimaryKey + ' < '
168 + '(SELECT MIN(' + @PrimaryKey + ') FROM (SELECT TOP '
169 + STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
170 +' FROM '+ @temptable
171 + @new_where1 + @new_order1 + ') AS TMP) '+ @new_order1
172 END
173 ELSE --反向检索
174 BEGIN
175 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('
176 + 'SELECT TOP ' + STR(@PageSize) + ' '
177 + @FieldList + ' FROM '
178 + @temptable + @new_where2 + @PrimaryKey + ' > '
179 + '(SELECT MAX(' + @PrimaryKey + ') FROM (SELECT TOP '
180 + STR(@TotalCount-@PageSize*@PageIndex) + ' ' + @PrimaryKey
181 + ' FROM ' + @temptable
182 + @new_where1 + @new_order2 +' ) AS TMP) '+ @new_order2
183 + ' ) AS TMP ' + @new_order1
184 END
185 END
186 IF @SortType = 3 --多列排序,必须包含主键,且放置最后,否则不处理
187 BEGIN
188 IF CHARINDEX(',' + @PrimaryKey + ' ',',' + @Order) = 0
189 BEGIN PRINT('ERR_02') RETURN END
190 IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向检索
191 BEGIN
192 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
193 + 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
194 + ' SELECT TOP ' + STR(@PageSize*@PageIndex) + ' ' + @FieldList
195 + ' FROM ' + @temptable + @new_where1 + @new_order1 + ' ) AS TMP '
196 + @new_order2 + ' ) AS TMP ' + @new_order1
197 END
198 ELSE --反向检索
199 BEGIN
200 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
201 + 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
202 + ' SELECT TOP ' + STR(@TotalCount-@PageSize *@PageIndex+@PageSize) + ' ' + @FieldList
203 + ' FROM ' + @temptable + @new_where1 + @new_order2 + ' ) AS TMP '
204 + @new_order1 + ' ) AS TMP ' + @new_order1
205 END
206 END
207 END
208 EXEC(@Sql)
209
1 CREATE PROC [dbo].[P_viewPage_A]
2 /*
3 nzperfect [no_mIss] 高效通用分页存储过程(双向检索) 2009.4.22
4 敬告:适用于单一主键或存在唯一值列的表或视图
5 ps:Sql语句为8000字节,调用时请注意传入参数及sql总长度不要超过指定范围
6 */
7 @TableName VARCHAR(200), --表名
8 @FieldList VARCHAR(2000), --显示列名,如果是全部字段则为*
9 @PrimaryKey VARCHAR(100), --单一主键或唯一值键
10 @Where VARCHAR(2000), --查询条件 不含'where'字符,如id>10 and len(userid)>9
11 @Order VARCHAR(1000), --排序(多列排序情况) 不含'order by'字符,如id asc,userid desc,必须指定asc或desc
12 --注意当@SortType=3时生效,记住一定要在最后加上主键,否则会让你比较郁闷
13 @SortType INT, --排序规则 1:正序asc 2:倒序desc 3:多列排序方法
14 @RecorderCount INT, --记录总数 0:会返回总记录
15 @PageSize INT, --每页输出的记录数
16 @PageIndex INT, --当前页数
17 @TotalCount INT OUTPUT, --记返回总记录
18 @TotalPageCount INT OUTPUT --返回总页数
19 AS
20 SET NOCOUNT ON
21 IF ISNULL(@TotalCount,'') = '' SET @TotalCount = 0
22 SET @Order = RTRIM(LTRIM(@Order))
23 SET @PrimaryKey = RTRIM(LTRIM(@PrimaryKey))
24 SET @FieldList = REPLACE(RTRIM(LTRIM(@FieldList)),' ','')
25 WHILE CHARINDEX(', ',@Order) > 0 OR CHARINDEX(' ,',@Order) > 0
26 BEGIN
27 SET @Order = REPLACE(@Order,', ',',')
28 SET @Order = REPLACE(@Order,' ,',',')
29 END
30 IF ISNULL(@TableName,'') = '' OR ISNULL(@FieldList,'') = ''
31 OR ISNULL(@PrimaryKey,'') = ''
32 OR @SortType < 1 OR @SortType >3
33 OR @RecorderCount < 0 OR @PageSize < 0 OR @PageIndex < 0
34 BEGIN
35 PRINT('ERR_00')
36 RETURN
37 END
38 IF @SortType = 3
39 BEGIN
40 IF (UPPER(RIGHT(@Order,4))!=' ASC' AND UPPER(RIGHT(@Order,5))!=' DESC')
41 BEGIN PRINT('ERR_02') RETURN END
42 END
43 DECLARE @new_where1 VARCHAR(1000)
44 DECLARE @new_where2 VARCHAR(1000)
45 DECLARE @new_order1 VARCHAR(1000)
46 DECLARE @new_order2 VARCHAR(1000)
47 DECLARE @new_order3 VARCHAR(1000)
48 DECLARE @temptable VARCHAR(1000)
49 DECLARE @Sql VARCHAR(8000)
50 DECLARE @SqlCount NVARCHAR(4000)
51 SET @temptable=@TableName
52 IF ISNULL(@where,'') = ''
53 BEGIN
54 SET @new_where1 = ' '
55 SET @new_where2 = ' WHERE '
56 END
57 ELSE
58 BEGIN
59 SET @new_where1 = ' WHERE ' + @where
60 SET @new_where2 = ' WHERE ' + @where + ' AND '
61 END
62 IF ISNULL(@order,'') = '' OR @SortType = 1 OR @SortType = 2
63 BEGIN
64 IF @SortType = 1
65 BEGIN
66 SET @new_order1 = ' ORDER BY ' + @PrimaryKey + ' ASC'
67 SET @new_order2 = ' ORDER BY ' + @PrimaryKey + ' DESC'
68 END
69 IF @SortType = 2
70 BEGIN
71 SET @new_order1 = ' ORDER BY ' + @PrimaryKey + ' DESC'
72 SET @new_order2 = ' ORDER BY ' + @PrimaryKey + ' ASC'
73 END
74 END
75 ELSE
76 BEGIN
77 SET @new_order1 = ' ORDER BY ' + @Order
78 END
79 IF @SortType = 3 AND CHARINDEX(','+@PrimaryKey+' ',','+@Order)>0
80 BEGIN
81 SET @new_order1 = ' ORDER BY ' + @Order
82 SET @new_order2 = @Order + ','
83 SET @new_order2 = REPLACE(REPLACE(@new_order2,'ASC,','{ASC},'),'DESC,','{DESC},')
84 SET @new_order2 = REPLACE(REPLACE(@new_order2,'{ASC},','DESC,'),'{DESC},','ASC,')
85 SET @new_order2 = ' ORDER BY ' + SUBSTRING(@new_order2,1,LEN(@new_order2)-1)
86 IF @FieldList <> '*'
87 BEGIN
88 SET @new_order3 = REPLACE(REPLACE(@Order + ',','ASC,',','),'DESC,',',')
89 SET @FieldList = ',' + @FieldList
90 WHILE CHARINDEX(',',@new_order3)>0
91 BEGIN
92 IF CHARINDEX(SUBSTRING(','+@new_order3,1,CHARINDEX(',',@new_order3)),','+@FieldList+',')>0
93 BEGIN
94 SET @FieldList =
95 @FieldList + ',' + SUBSTRING(@new_order3,1,CHARINDEX(',',@new_order3))
96 END
97 SET @new_order3 =
98 SUBSTRING(@new_order3,CHARINDEX(',',@new_order3)+1,LEN(@new_order3))
99 END
100 SET @FieldList = SUBSTRING(@FieldList,2,LEN(@FieldList))
101 END
102 END
103 --根据所要取的记录数@RecorderCount取出总记录数及总页数
104 SET @SqlCount = 'SELECT @TotalCount=COUNT(*),@TotalPageCount=CEILING((COUNT(*)+0.0)/'
105 + CAST(@PageSize AS VARCHAR)+') FROM ' + @temptable + @new_where1
106 IF @RecorderCount = 0
107 BEGIN
108 EXEC SP_EXECUTESQL @SqlCount,N'@TotalCount INT OUTPUT,@TotalPageCount INT OUTPUT',
109 @TotalCount OUTPUT,@TotalPageCount OUTPUT
110 END
111 ELSE
112 BEGIN
113 SELECT @TotalCount = @RecorderCount,@TotalPageCount=CEILING((@TotalCount+0.0)/@PageSize)
114 set @SqlCount='drop table temp select TOP('+CAST(@TotalCount As varchar)+') * into temp from '+@temptable+ @new_where1 + @new_order1
115 exec sp_executesql @SqlCount
116 set @temptable='temp'
117 END
118 --取数据
119 IF @PageIndex = 1 OR @PageIndex >= CEILING((@TotalCount+0.0)/@PageSize) OR @PageIndex =0
120 BEGIN
121 IF @PageIndex = 1 OR @PageIndex =0 --返回第一页数据
122 BEGIN
123 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
124 + @temptable + @new_where1 + @new_order1
125 END
126 IF @PageIndex >= CEILING((@TotalCount+0.0)/@PageSize) --返回最后一页数据
127 BEGIN
128 SET @PageIndex = CEILING((@TotalCount+0.0)/@PageSize)
129 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('
130 + 'SELECT TOP ' + STR(ABS(@PageSize*@PageIndex-@TotalCount-@PageSize))
131 + ' ' + @FieldList + ' FROM '
132 + @temptable + @new_where1 + @new_order2 + ' ) AS TMP '
133 + @new_order1
134 END
135 END
136 ELSE
137 BEGIN
138 IF @SortType = 1 --仅主键正序排序
139 BEGIN
140 IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向检索
141 BEGIN
142 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
143 + @temptable + @new_where2 + @PrimaryKey + ' > '
144 + '(SELECT MAX(' + @PrimaryKey + ') FROM (SELECT TOP '
145 + STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
146 + ' FROM ' + @temptable
147 + @new_where1 + @new_order1 +' ) AS TMP) '+ @new_order1
148 END
149 ELSE --反向检索
150 BEGIN
151 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('
152 + 'SELECT TOP ' + STR(@PageSize) + ' '
153 + @FieldList + ' FROM '
154 + @temptable + @new_where2 + @PrimaryKey + ' < '
155 + '(SELECT MIN(' + @PrimaryKey + ') FROM (SELECT TOP '
156 + STR(@TotalCount-@PageSize*@PageIndex) + ' ' + @PrimaryKey
157 + ' FROM ' + @temptable
158 + @new_where1 + @new_order2 +' ) AS TMP) '+ @new_order2
159 + ' ) AS TMP ' + @new_order1
160 END
161 END
162 IF @SortType = 2 --仅主键反序排序
163 BEGIN
164 IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向检索
165 BEGIN
166 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
167 + @temptable + @new_where2 + @PrimaryKey + ' < '
168 + '(SELECT MIN(' + @PrimaryKey + ') FROM (SELECT TOP '
169 + STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
170 +' FROM '+ @temptable
171 + @new_where1 + @new_order1 + ') AS TMP) '+ @new_order1
172 END
173 ELSE --反向检索
174 BEGIN
175 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ('
176 + 'SELECT TOP ' + STR(@PageSize) + ' '
177 + @FieldList + ' FROM '
178 + @temptable + @new_where2 + @PrimaryKey + ' > '
179 + '(SELECT MAX(' + @PrimaryKey + ') FROM (SELECT TOP '
180 + STR(@TotalCount-@PageSize*@PageIndex) + ' ' + @PrimaryKey
181 + ' FROM ' + @temptable
182 + @new_where1 + @new_order2 +' ) AS TMP) '+ @new_order2
183 + ' ) AS TMP ' + @new_order1
184 END
185 END
186 IF @SortType = 3 --多列排序,必须包含主键,且放置最后,否则不处理
187 BEGIN
188 IF CHARINDEX(',' + @PrimaryKey + ' ',',' + @Order) = 0
189 BEGIN PRINT('ERR_02') RETURN END
190 IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向检索
191 BEGIN
192 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
193 + 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
194 + ' SELECT TOP ' + STR(@PageSize*@PageIndex) + ' ' + @FieldList
195 + ' FROM ' + @temptable + @new_where1 + @new_order1 + ' ) AS TMP '
196 + @new_order2 + ' ) AS TMP ' + @new_order1
197 END
198 ELSE --反向检索
199 BEGIN
200 SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
201 + 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM ( '
202 + ' SELECT TOP ' + STR(@TotalCount-@PageSize *@PageIndex+@PageSize) + ' ' + @FieldList
203 + ' FROM ' + @temptable + @new_where1 + @new_order2 + ' ) AS TMP '
204 + @new_order1 + ' ) AS TMP ' + @new_order1
205 END
206 END
207 END
208 EXEC(@Sql)
209