会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
单身秀财
博客园
首页
新随笔
联系
订阅
管理
通用的分页储存过程
Code
1
--
获取指定页的数据
2
CREATE
PROCEDURE
dbo.pagination
3
4
@tblName
varchar
(
255
),
--
表名
5
@strGetFields
varchar
(
1000
)
=
'
*
'
,
--
需要返回的列
6
@fldName
varchar
(
255
)
=
''
,
--
排序的字段名
7
@PageSize
int
=
10
,
--
页尺寸
8
@PageIndex
int
=
1
,
--
页码
9
@doCount
bit
=
0
,
--
返回记录总数, 非 0 值则返回
10
@OrderType
bit
=
0
,
--
设置排序类型, 非 0 值则降序
11
@strWhere
varchar
(
1500
)
=
''
--
查询条件 (注意: 不要加 where)
12
13
AS
14
15
declare
@strSQL
varchar
(
5000
)
--
主语句
16
declare
@strTmp
varchar
(
110
)
--
临时变量
17
declare
@strOrder
varchar
(
400
)
--
排序类型
18
if
@doCount
!=
0
19
begin
20
if
@strWhere
!=
''
21
set
@strSQL
=
'
select count(*) as Total from [
'
+
@tblName
+
'
] where
'
+
@strWhere
22
else
23
set
@strSQL
=
'
select count(*) as Total from [
'
+
@tblName
+
'
]
'
24
end
25
--
以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
26
27
else
28
begin
29
if
@OrderType
!=
0
30
begin
31
set
@strTmp
=
'
<(select min
'
32
set
@strOrder
=
'
order by [
'
+
@fldName
+
'
] desc
'
33
--
如果@OrderType不是0,就执行降序,这句很重要!
34
end
35
36
else
37
begin
38
set
@strTmp
=
'
>(select max
'
39
set
@strOrder
=
'
order by [
'
+
@fldName
+
'
] asc
'
40
end
41
if
@PageIndex
=
1
42
begin
43
if
@strWhere
!=
''
44
set
@strSQL
=
'
select top
'
+
str
(
@PageSize
)
+
'
'
+
@strGetFields
+
'
from [
'
+
@tblName
+
'
] where
'
+
@strWhere
+
'
'
+
@strOrder
45
else
46
set
@strSQL
=
'
select top
'
+
str
(
@PageSize
)
+
'
'
+
@strGetFields
+
'
from [
'
+
@tblName
+
'
]
'
+
@strOrder
47
--
如果是第一页就执行以上代码,这样会加快执行速度
48
end
49
50
else
51
begin
52
--
以下代码赋予了@strSQL以真正执行的SQL代码
53
set
@strSQL
=
'
select top
'
+
str
(
@PageSize
)
+
'
'
+
@strGetFields
+
'
from [
'
54
+
@tblName
+
'
] where [
'
+
@fldName
+
'
]
'
+
@strTmp
+
'
([
'
+
@fldName
+
'
]) from (select top
'
+
str
((
@PageIndex
-
1
)
*
@PageSize
)
+
'
[
'
+
@fldName
+
'
] from [
'
+
@tblName
+
'
]
'
+
@strOrder
+
'
) as tblTmp)
'
+
@strOrder
55
if
@strWhere
!=
''
56
set
@strSQL
=
'
select top
'
+
str
(
@PageSize
)
+
'
'
+
@strGetFields
+
'
from [
'
57
+
@tblName
+
'
] where [
'
+
@fldName
+
'
]
'
+
@strTmp
+
'
([
'
58
+
@fldName
+
'
]) from (select top
'
+
str
((
@PageIndex
-
1
)
*
@PageSize
)
+
'
[
'
59
+
@fldName
+
'
] from [
'
+
@tblName
+
'
] where
'
+
@strWhere
+
'
'
60
+
@strOrder
+
'
) as tblTmp) and
'
+
@strWhere
+
'
'
+
@strOrder
61
end
62
end
63
exec
(
@strSQL
)
64
GO
65
posted on
2009-05-06 18:36
单身秀财
阅读(
155
) 评论(
0
)
收藏
举报
刷新页面
返回顶部