通用的翻页存储过程

Declare @CurrentPage int 
Declare @PageSize int 
set @PageSize=6000
set @CurrentPage=0

Declare @ID int 
Declare @MoveRecords int 


--@CurrentPage和@PageSize是传入参数 
Set @MoveRecords=@CurrentPage * @PageSize+1 

--下面两行实现快速滚动到我们要取的数据的行,并把ID记录下来 
Set Rowcount @MoveRecords 
Select @ID=ID from news Order by ID 

Set Rowcount @PageSize 
--
Select * From news Where ID>=@ID Order By ID 
Set Rowcount 0 
create procedure prc_Test
(
    
@PageSize int,            --定义页面的大小
    @PageIndex int,           --定义当前页面的索引
    @strWhere nvarchar(1000)  --定义查询条件
)
as
begin
    
declare @StartID int
    
declare @MoveRecords int
    
declare @strTempSql nvarchar(1000)

    
set @MoveRecords = @PageIndex * @PageSize +1
    
set rowcount @MoveRecords
    
set @strTempSql='select @StartID=id from PageTest where ' + @strWhere
    
exec sp_executesql @strTempSql,N'@StartID int output',@StartID output   
    
set rowcount @PageSize
    
exec('select ID,Number,Name from PageTest where ' + @strWhere)
    
set rowcount 0
end 
CREATE PROCEDURE  Company
(
@PageSize int--每页的记录数
@PageIndex int,  --当前页码
@StrWhere varchar(1000--查询条件
)
 
AS
set nocount on
begin
create table #aa
(
id 
int identity(1,1),
nid 
varchar(50)
)
declare @PageLowerBound int --定义下限
declare @PageUpperBound int --定义上限
set @PageLowerBound=(@PageIndex-1)*@PageSize
set @PageUpperBound=@PageLowerBound+@PageSize
set rowcount @PageUpperBound
declare @SqlStr varchar(1000)
set @SqlStr='insert into #aa(nid) select khbh from Company where '+@StrWhere+' order by Date1 desc'
exec (@SqlStr)
select * from Company a where a.khbh in (select nid from #aa b where a.khbh=b.nid and b.id between @PageLowerBound+1 and @PageUpperBoundorder by Date1 desc
end 
set nocount off
GO
posted @ 2007-08-01 10:12  你约我交友网  阅读(253)  评论(0)    收藏  举报