执行存储过程返回记录,如何分页?
如题.执行存储过程返回记录,如何分页?
返回后用
RS.pagesize = 20
提示该记录集不支持书签.怎么办?
问题点数:20、回复次数:3Top
1 楼iuhxq(小灰)回复于 2005-08-27 00:37:55 得分 20
用存储过程可以直接分页呀,不用recordset了
抛砖引玉:
CREATE proc page
--@t int output,--------------------------------------------------------------------------------------------
@RecordCount int output,
@QueryStr nvarchar(100)='table1',--表名、视图名、查询语句
@PageSize int=20, --每页的大小(行数)
@PageCurrent int=2, --要显示的页 从0开始
@FdShow nvarchar (1000)='*', --要显示的字段列表
@IdentityStr nvarchar (100)='id', --主键
@WhereStr nvarchar (200)='1=1',
@FdOrder nvarchar(100)='desc' --排序 只能取desc或者asc
as
set nocount on
declare
--@t1 datetime ,---------------------------------------------------------------------------------------------------------------------
@sql nvarchar(2000)
--set @t1 = getdate()----------------------------------------------------------------------------------------------
if @WhereStr = '' begin
set @WhereStr = '1=1'
end
if(@RecordCount is null or @RecordCount<=0)begin
declare @tsql nvarchar(200)
set @tsql=N'select @RecordCount = count(*) from ' + @QueryStr + ' where ' + @WhereStr
exec sp_executesql @tsql,N'@RecordCount int output',@RecordCount output
end
if @PageCurrent = 0 begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' ' + @FdOrder
end
else begin
if upper(@FdOrder) = 'DESC' begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '< ( select min(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' desc) as t) order by ' + @IdentityStr + ' desc'
end
else begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '> ( select max(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' asc) as t) order by ' + @IdentityStr + ' asc'
end
end
--print @sql
execute(@sql)
--select @t = datediff(ms,@t1,getdate())---------------------------------------------------------------------------
GO
Top
2 楼zs1005()回复于 2005-08-27 00:54:42 得分 0
谢谢.可是目前有这个需要.
有很多存储过程.一时没时间写这么多这么复杂的.
如果非要将返回的记录集分页再分页,应该怎么做呢?Top
3 楼jycjyc(果果)回复于 2005-12-22 08:57:16 得分 0
收Top




