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
查看全文
相关阅读:
Fix Installing .NET Framework 3.5 failed Error Code 0x800F0954 on Windows 10
RHEL8安装五笔输入法
Enable EPEL and Local Repository on RHEL8
Why is Yum Replaced by DNF?
检查Linux服务器是否被攻击的常用命令及方法
IDEA 主题
IDEA 如何显示一个类中所有的方法
Appium 安装以及安装过程中遇到的问题
Maven 如何发布 jar 包到 Nexus 私库
java泛型的基本使用
原文地址:https://www.cnblogs.com/snowball/p/1039689.html
最新文章
vsCode 启动本地服务器插件 Live Server
清缓存的两种方法:webpack清缓存
el-tree 回显默认传值
el-form rules校验清除
elementui禁用全选按钮 使用原生去禁止
读【技术人员需要有产品思维吗】摘抄
Spring和SpringBoot中的@Component 和@ComponentScan注解用法介绍和注意事项
springboot有哪些常用注解?
kong管理界面konga的安装
CentOS 7/8上部署Ceph
热门文章
汉语-词汇:情景
人性-人性假说:社会人
人性-人性假说:经济人
人性-人性假说:复杂人
安全-服务器:双机热备
安全-性能-数据库:读写分离
Error-IIS-ASP.NET:Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started and that the client and server ports are the same.
软件度量:内聚性
软件:软件度量
笔记:测验笔记-20200831
Copyright © 2011-2022 走看看