zoukankan
html css js c++ java
通用数据库存储过程代码高效分页存储过程
--
获取指定页的数据
Create
PROCEDURE
pagination
@tblName
varchar
(
255
),
--
表名
@strGetFields
varchar
(
1000
)
=
'
*
'
,
--
需要返回的列
@fldName
varchar
(
255
)
=
''
,
--
排序的字段名
@PageSize
int
=
10
,
--
页尺寸
@PageIndex
int
=
1
,
--
页码
@doCount
bit
=
0
,
--
返回记录总数, 非 0 值则返回
@OrderType
bit
=
0
,
--
设置排序类型, 非 0 值则降序
@strWhere
varchar
(
1500
)
=
''
--
查询条件 (注意: 不要加 where)
AS
declare
@strSQL
varchar
(
5000
)
--
主语句
declare
@strTmp
varchar
(
110
)
--
临时变量
declare
@strOrder
varchar
(
400
)
--
排序类型
if
@doCount
!=
0
begin
if
@strWhere
!=
''
set
@strSQL
=
"
select
count
(
*
)
as
Total
from
[
" + @tblName + "
]
where
"
+
@strWhere
else
set
@strSQL
=
"
select
count
(
*
)
as
Total
from
[
" + @tblName + "
]
"
end
--
以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
else
begin
if
@OrderType
!=
0
begin
set
@strTmp
=
"
<
(
select
min
"
set
@strOrder
=
"
order
by
[
" + @fldName +"
]
desc
"
--
如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set
@strTmp
=
"
>
(
select
max
"
set
@strOrder
=
"
order
by
[
" + @fldName +"
]
asc
"
end
if
@PageIndex
=
1
begin
if
@strWhere
!=
''
set
@strSQL
=
"
select
top
"
+
str
(
@PageSize
)
+
" "
+
@strGetFields
+
"
from
[
" + @tblName + "
]
where
"
+
@strWhere
+
" "
+
@strOrder
else
set
@strSQL
=
"
select
top
"
+
str
(
@PageSize
)
+
" "
+
@strGetFields
+
"
from
[
"+ @tblName + "
]
"
+
@strOrder
--
如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin
--
以下代码赋予了@strSQL以真正执行的SQL代码
set
@strSQL
=
"
select
top
"
+
str
(
@PageSize
)
+
" "
+
@strGetFields
+
"
from
[
"
+ @tblName + "
]
where
[
" + @fldName + "
]
"
+
@strTmp
+
"(
[
"+ @fldName + "
]
)
from
(
select
top
"
+
str
((
@PageIndex
-
1
)
*
@PageSize
)
+
"
[
"+ @fldName + "
]
from
[
" + @tblName + "
]
"
+
@strOrder
+
")
as
tblTmp)"
+
@strOrder
if
@strWhere
!=
''
set
@strSQL
=
"
select
top
"
+
str
(
@PageSize
)
+
" "
+
@strGetFields
+
"
from
[
"
+ @tblName + "
]
where
[
" + @fldName + "
]
"
+
@strTmp
+
"(
[
"
+ @fldName + "
]
)
from
(
select
top
"
+
str
((
@PageIndex
-
1
)
*
@PageSize
)
+
"
[
"
+ @fldName + "
]
from
[
" + @tblName + "
]
where
"
+
@strWhere
+
" "
+
@strOrder
+
")
as
tblTmp)
and
"
+
@strWhere
+
" "
+
@strOrder
end
end
exec
(
@strSQL
)
GO
Dynamic Sql
Code
declare
@ParameterC
nvarchar
(
100
)
exec
SP_EXECUTESQL
N
'
select @ParameterA,@ParameterB
'
,
N
'
@ParameterA int,@ParameterB int
'
,
@Parametera
=
1
,
@Parameterb
=
2
select
@ParameterC
declare
@sql
nvarchar
(
100
)
declare
@R
nvarchar
(
100
)
declare
@ParameterA
int
--
set @SQL= N'select @R=11,@ParameterA'
--
exec SP_EXECUTESQL @SQL, N' @R BIGINT OUTPUT,@ParameterA int', @R OUTPUT,@ParameterA=123
set
@SQL
=
N
'
select @R=11
'
exec
SP_EXECUTESQL
@SQL
, N
'
@R BIGINT OUTPUT
'
,
@R
OUTPUT
select
@R
查看全文
相关阅读:
java学习笔记-设计模式12(组合模式)
Java神奇的装箱与拆箱
博客得不到搜索引擎的收录!
Java中E、T、K、V、N的含义
【转】解决Ubuntu下Sublime Text 3无法输入中文
【转】Windows和Ubuntu双系统,修复UEFI引导的两种办法
30分钟学会正则表达式
推荐一个android各种工具的墙内下载网站
如何为Android Studio 添加快速启动方式【Linux】
Linux 更改用户环境变量和所有用户环境变量
原文地址:https://www.cnblogs.com/snowball/p/1039689.html
最新文章
chrome前端插件推荐
函数表达式和函数声明
javascript 中的 delete
apply()和call()和bind()
判断一个对象是不是数组
JavaScript中__proto__与prototype的关系
高度塌陷问题
如何解决外边距叠加的问题?
HTML标签之<q> <blockquote>
.使用 HTML+CSS 实现如图布局,border-widht 5px,一个格子大小是 50*50,hover时候边框变为红色(兼容IE6+,考虑语义化的结构)
热门文章
如何垂直居中一个<img>?
java学习笔记-设计模式21(状态模式)
java学习笔记-设计模式20(备忘录模式)
java学习笔记-设计模式19(命令模式)
java学习笔记-设计模式18(职责链模式)
java学习笔记-设计模式17(迭代器模式)
java学习笔记-设计模式16(观察者模式)
java学习笔记-设计模式15(模板方法模式)
java学习笔记-设计模式14(策略模式)
java学习笔记-设计模式13(享元模式)
Copyright © 2011-2022 走看看