使用存储过程分页
大家知道,如果一次性把记录全部查询过来,然后保存在内存里面,会比较浪费资源,因此,需要多少数据查询多少数据才比较合理,下面是使用存储过程分页的方法。这个方法的缺点就是如果到记录的后面时,查询的量也比较大。
--根据条件显示相应的记录,并分页。
CREATE PROC SelectPageFiles
@WhereCondition nvarchar(500), --查询条件
@PageIndex int, --页号
@PageCount int, --每页的记录数
@OrderCondition varchar(50) --排序条件
as
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
declare @SQL nvarchar(2500)
set @SQL='SELECT top '+convert(varchar,@PageCount)+' FileId,
[title],
UpFile,
filetable.cateid,
filetable.gradeId,
[adddate],
filetable.filetypeid,
[kemu_id],
hit,
dbo.GetGradeName(gradeId) as GradeName,
dbo.GetFileTypeName(FileTypeID) as FileTypeName,
convert(int,FileSize) as FileSize2
FROM [dbo].[filetable]
WHERE fileid not in
(
select top '+convert(varchar,(@PageIndex-1)*@PageCount)+' fileid from filetable
where '+@WhereCondition+' order by '+@OrderCondition+')
and '+@WhereCondition+' order by '+@OrderCondition
print @SQL
EXEC sp_executesql @SQL
GO
CREATE PROC SelectPageFiles
@WhereCondition nvarchar(500), --查询条件
@PageIndex int, --页号
@PageCount int, --每页的记录数
@OrderCondition varchar(50) --排序条件
as
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
declare @SQL nvarchar(2500)
set @SQL='SELECT top '+convert(varchar,@PageCount)+' FileId,
[title],
UpFile,
filetable.cateid,
filetable.gradeId,
[adddate],
filetable.filetypeid,
[kemu_id],
hit,
dbo.GetGradeName(gradeId) as GradeName,
dbo.GetFileTypeName(FileTypeID) as FileTypeName,
convert(int,FileSize) as FileSize2
FROM [dbo].[filetable]
WHERE fileid not in
(
select top '+convert(varchar,(@PageIndex-1)*@PageCount)+' fileid from filetable
where '+@WhereCondition+' order by '+@OrderCondition+')
and '+@WhereCondition+' order by '+@OrderCondition
print @SQL
EXEC sp_executesql @SQL
GO