二种MSSQL分页存储过程实例应用
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>二种MSSQL分页存储过程实例应用</title>
</head>
<body>
MSSQL分页在小型数据网站应用中,不需要用到存存储过程,只有上万百,千万级数据库教程时就会用到存储过程,
创建存储过程
CREATE PROCEDURE pagination
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名(可包含如TABLE.FLDNAME形式)
@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) -- 排序类型
declare @fldName_t varchar(255) -- 在分页时用的排序字段名,不包含多表并列时的表名
set @fldName_t = right(@fldName,len(@fldName)-CHARINDEX('.',@fldName))
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_t + ') 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_t + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '+ @fldName + ' from ' + @tblName + ' where ' + @strWhere + ' '+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
end
exec (@strSQL)
go
--测试
create table news --建表
(
n_id int iDENTITY(1,1) primary key,
n_title char(200),
n_content text
)
--写循环插入1000000条的数据
create proc tt
as
declare @i int
set @i=0
while(@i<1000000)
begin
insert into news(n_title,n_content) values('sb','dsfsdfsd')
set @i=@i+1
end
exec tt
exec pagination 'news','*','n_id',1000,2,0,0,''
MSSQL分页存储过程,支持连接查询等复杂的查询
调用方法
exec Pagination 'select * from [order] ', 20, 0, 'CreateTime ASC'
exec Pagination 'select * from [order] ', 20, 0, 'CreateTime ASC, Money DESC'
实例过程
CREATE PROCEDURE Pagination
(
@SQL nvarchar(1024), --查询语句
@PageSize int = 20, --分页大小
@PageIndex int = 0, --分页索引
@Sort nvarchar(100) = '', --排序字段
@TotalCount int = 0 output --总数
)
AS
set nocount on
/*声明查询字符串*/
declare @strSQL nvarchar(4000)
set @strSQL = ' select @TotalCount=count(*) from ('+@SQL+') as t '
/*取得查询结果总数*/
exec sp_executesql
@strSQL,
N'@TotalCount int=0 OUTPUT',
@TotalCount=@TotalCount OUTPUT
declare @ItemCount int
declare @_PageIndex int
set @_PageIndex = @PageIndex + 1;
/*确定搜索边界*/
set @ItemCount = @TotalCount - @PageSize * @_PageIndex
if(@ItemCount < 0)
set @ItemCount = @ItemCount + @PageSize
else
set @ItemCount = @PageSize
if(@ItemCount < 0) return 1
if(@Sort != '')
begin
/*声明排序变量*/
declare @IndexSort1 nvarchar(50), @IndexSort2 nvarchar(50), @Sort1 nvarchar(50), @Sort2 nvarchar(50)
SET @Sort1 = @Sort
SET @Sort2 = Replace(Replace(Replace(@Sort, 'DESC', '@SORT'), 'ASC', 'DESC'), '@SORT', 'ASC')
set @strSQL = 'SELECT * FROM
(SELECT TOP ' + STR(@ItemCount) + ' * FROM
(SELECT TOP ' + STR(@PageSize * @_PageIndex) + ' * FROM
('+@SQL+') AS t0
ORDER BY '+@Sort1 +') AS t1
ORDER BY '+@Sort2 +') AS t2
ORDER BY ' +@Sort
end
else
begin
set @strSQL = 'SELECT * FROM
(SELECT TOP ' + STR(@ItemCount) + ' * FROM
(SELECT TOP ' + STR(@PageSize * @_PageIndex) + ' * FROM
('+@SQL+') As t0)
aS t1)
AS t2'
end
exec sp_executesql
@strSQL
GO
<?
/*
存储过程好处是,函数是mssql内部函数所在运行起来是很快的。
*/?>
</body>
</html>