数据分页 存储过程
来源:互联网 发布:飘谁翻译的最好 知乎 编辑:程序博客网 时间:2024/04/29 02:00
IF EXISTS(SELECT * FROM sys.objects WHERE [NAME] = 'Pagination')
DROP PROCEDURE Pagination
GO
CREATE PROCEDURE Pagination
@tblName NVARCHAR(255),-- 表名
@strGetFields NVARCHAR(1000) = '*',-- 需要返回的列
@fldName NVARCHAR(255)='',-- 排序的字段名
@PageSize INT = 10,-- 页尺寸
@PageIndex INT = 1,-- 页码
@doCount BIT = 0,-- 返回记录总数, 非 0 值则返回
@OrderType BIT = 0,-- 设置排序类型, 非 0 值则降序
@strWhere NVARCHAR(1500) = '' -- 查询条件 (注意: 不要加 where)
AS
DECLARE @strSQL NVARCHAR(4000)-- 主语句
DECLARE @strTmp NVARCHAR(110)-- 临时变量
DECLARE @strOrder NVARCHAR(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) +' [email=[ft=,2,]'+@strGetFields]'+@strGetFields[/email]+ ' from ['+ @tblName +'] where ' + @strWhere + ' ' + @strOrder
ELSE
SET @strSQL = 'select top ' + str(@PageSize) +' [email=[ft=,2,]'+@strGetFields]'+@strGetFields[/email]+ ' from ['+ @tblName +'] '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
END
ELSE
BEGIN
--以下代码赋予了@strSQL以真正执行的SQL代码
SET @strSQL = 'select top ' + str(@PageSize) +' [email=[ft=,2,]'+@strGetFields]'+@strGetFields[/email]+ ' 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) +' [email=[ft=,2,]'+@strGetFields]'+@strGetFields[/email]+ ' 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
GO
CREATE PROCEDURE Pagination
@tblName NVARCHAR(255),-- 表名
@strGetFields NVARCHAR(1000) = '*',-- 需要返回的列
@fldName NVARCHAR(255)='',-- 排序的字段名
@PageSize INT = 10,-- 页尺寸
@PageIndex INT = 1,-- 页码
@doCount BIT = 0,-- 返回记录总数, 非 0 值则返回
@OrderType BIT = 0,-- 设置排序类型, 非 0 值则降序
@strWhere NVARCHAR(1500) = '' -- 查询条件 (注意: 不要加 where)
AS
DECLARE @strSQL NVARCHAR(4000)-- 主语句
DECLARE @strTmp NVARCHAR(110)-- 临时变量
DECLARE @strOrder NVARCHAR(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) +' [email=[ft=,2,]'+@strGetFields]'+@strGetFields[/email]+ ' from ['+ @tblName +'] where ' + @strWhere + ' ' + @strOrder
ELSE
SET @strSQL = 'select top ' + str(@PageSize) +' [email=[ft=,2,]'+@strGetFields]'+@strGetFields[/email]+ ' from ['+ @tblName +'] '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
END
ELSE
BEGIN
--以下代码赋予了@strSQL以真正执行的SQL代码
SET @strSQL = 'select top ' + str(@PageSize) +' [email=[ft=,2,]'+@strGetFields]'+@strGetFields[/email]+ ' 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) +' [email=[ft=,2,]'+@strGetFields]'+@strGetFields[/email]+ ' 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
- 数据分页存储过程
- 数据分页 存储过程
- 数据分页 存储过程
- 数据分页存储过程
- 数据分页存储过程
- 大量数据分页存储过程
- 数据分页的存储过程
- 海量数据分页存储过程
- 存储过程进行数据分页
- SQL优化(索引、存储过程、数据分页的存储过程)
- 一个将数据分页的存储过程
- 一个将数据分页的存储过程
- 一个将数据分页的存储过程
- 将数据分页的存储过程
- asp 用存储过程实现数据分页
- 一个将数据分页的存储过程
- 一个简单的存储过程数据分页
- 一个简单的存储过程数据分页
- Labview 子VI 生成Library Files
- java中文乱码解决方法之转码(gb2312转ISO)
- Jsp中的session使用
- 18位身份证验证小组件idchecker发布
- C#与MFC的简单线程
- 数据分页 存储过程
- tomcat java.lang.OutOfMemoryError: Java heap space 问题解决;Java垃圾回收机制详解和调优相关链接
- 硬盘、U盘起死回生的方法:文件系统显示为RAW文件系统,总共字节为0,可用字节为0且无法读取 的解决办法
- 输入密码显示**
- 移植uCOS ii到ARM920T
- c# 启动单个程序
- ruby关于检查IP地址正则的例子
- JAVA与C#比较
- linux下挂载windows虚拟目录