博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分页存储过程
阅读量:6908 次
发布时间:2019-06-27

本文共 2258 字,大约阅读时间需要 7 分钟。

      
varchar
(
255
),       
--
 表名
    
@fldName
      
varchar
(
255
),       
--
 字段名
    
@PageSize
     
int
 
=
 
10
,           
--
 页尺寸
    
@PageIndex
    
int
 
=
 
1
,            
--
 页码
    
@IsCount
      
bit
 
=
 
0
,            
--
 返回记录总数, 非 0 值则返回
    
@OrderType
    
bit
 
=
 
0
,            
--
 设置排序类型, 非 0 值则降序
    
@strWhere
     
varchar
(
5000
=
 
''
  
--
 查询条件 (注意: 不要加 where)
AS
/**/
/**********************************
Author:xuty
date:20070706
descript:解密exec sp_decrypt 'GetRecordFromPage'
    GetRecordFromPage得到
Date:2007-07-06
@strWhere:最多只能存3500字节,不然系统可能会出错
    
*********************************
*/
declare
 
@strSQL
   
varchar
(
7999
)       
--
 主语句
declare
 
@strTmp
   
varchar
(
5000
)        
--
 临时变量
declare
 
@strOrder
 
varchar
(
400
)        
--
 排序类型
declare
 
@counts
  
int
declare
 
@strPageNum
 
nvarchar
(
4000
declare
 
@PageNum
 
int
if
 
@OrderType
 
!=
 
0
begin
    
set
 
@strTmp
 
=
 
'
<(select min
'
    
set
 
@strOrder
 
=
 
'
 order by [
'
 
+
 
@fldName
 
+
'
] desc
'
end
else
begin
    
set
 
@strTmp
 
=
 
'
>(select max
'
    
set
 
@strOrder
 
=
 
'
 order by [
'
 
+
 
@fldName
 
+
'
] asc
'
end
set
 
@strSQL
 
=
 
'
select top 
'
 
+
 
str
(
@PageSize
+
 
'
 * 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
+
 
'
 * from [
'
        
+
 
@tblName
 
+
 
'
] where [
'
 
+
 
@fldName
 
+
 
'
]
'
 
+
 
@strTmp
 
+
 
'
([
'
        
+
 
@fldName
 
+
 
'
]) from (select top 
'
 
+
 
str
((
@PageIndex
-
1
)
*
@PageSize
+
 
'
 [
'
        
+
 
@fldName
 
+
 
'
] from [
'
 
+
 
@tblName
 
+
 
'
] where 
'
 
+
 
@strWhere
 
+
 
'
 
'
        
+
 
@strOrder
 
+
 
'
) as tblTmp) and 
'
 
+
 
@strWhere
 
+
 
'
 
'
 
+
 
@strOrder
--
if
 
@PageIndex
 
=
 
1
begin
    
set
 
@strTmp
 
=
 
''
    
if
 
@strWhere
 
!=
 
''
        
set
 
@strTmp
 
=
 
'
 where 
'
 
+
 
@strWhere
    
set
 
@strSQL
 
=
 
'
select top 
'
 
+
 
str
(
@PageSize
+
 
'
 * from [
'
        
+
 
@tblName
 
+
 
'
]
'
 
+
 
@strTmp
 
+
 
'
 
'
 
+
 
@strOrder
end
if
 
@IsCount
 
!=
 
0
    
set
 
@strSQL
 
=
 
'
select count(*) as Total from [
'
 
+
 
@tblName
 
+
 
'
]
'
if
 
@strWhere
 
!=
 
''
begin
       
set
 
@strPageNum
 
=
 
'
select @counts=count(*) from [
'
 
+
 
@tblName
 
+
 
'
]
'
 
+
 
'
 where 
'
+
@strWhere
end
 
else
begin
    
set
 
@strPageNum
 
=
 
'
select @counts=count(*) from [
'
 
+
 
@tblName
 
+
 
'
]
'
 
end
exec
 sp_executesql 
@strPageNum
,N
'
@Counts int out 
'
,
@Counts
 out
if
 
@Counts
 
<=
 
@pageSize
    
set
 
@PageNum
 
=
 
1
else
    
set
 
@PageNum
 
=
 (
@Counts
 
/
 
@pageSize
+
 
1
exec
 (
@strSQL
)
select
 
@PageNum
GO

转载地址:http://omgdl.baihongyu.com/

你可能感兴趣的文章
压缩并备份Access数据库
查看>>
Loadrunner 安装
查看>>
python分析nginx日志并推送到open-falcon
查看>>
纸上得来终觉浅,记IBM X3650 M3配置RAID0并安装EXSi5
查看>>
bboss标签使用大全-数据展示标签
查看>>
java中hashCode()与equals()详解
查看>>
点滴积累【JS】---JS小功能(createElement和insertBefore添加div下面的节点)
查看>>
异步提交form表单
查看>>
A Newbie’s Install of Keras & Tensorflow on Windows 10 with R
查看>>
关于使用input type=file 标签上传文件的注意细节(上传文件 无法获取文件 问题)...
查看>>
<if test="outState!=null">OUT_STATE=#{outState},</if>空格问题
查看>>
PHP~foreach遍历名单数组~有必要多次观看练习
查看>>
.Net内存回收
查看>>
js 获取/设置文本输入域内光标的位置的方法
查看>>
oracle sql developer 出现 : 适配器无法建立连接问题解决方案 The Network Adapter could not establish the connection...
查看>>
Linux下connect超时处理【总结】
查看>>
高性能数据库集群:读写分离
查看>>
Laravel 5.5 Blade::if 简介
查看>>
centos7搭建ELK Cluster集群日志分析平台(三):Kibana
查看>>
UITextField 监听内容变更解决方案
查看>>