自己写的SQL存储过程分页方法

原文链接: http://www.cnblogs.com/it563/archive/2008/03/28/1126778.html
CREATE   PROC  page 
@count   int  out,
@PageIndex   int ,
@PageCount   int  out
AS
SELECT   @count   =   COUNT (id)  FROM  user1
set   @PageCount   = floor ( @count / 10 + 1 )
DECLARE   @SQLSTR   NVARCHAR ( 1000
if   @PageIndex = 0   or   @PageCount <= 1
begin
SET   @SQLSTR   = ' select top 10  *  from user1 order by id asc '
end
else
begin
if   @PageIndex   = @PageCount - 1
    
begin
SET   @SQLSTR   = ' select top 10  * from user1 where id not in(select top  ' + STR ( @PageCount * 10 - 10 ) + '  id from user1) order by id asc '
    
end
else
begin
SET   @SQLSTR   = ' select top 10 *  from user1 where id not in(select top  ' + STR ( @PageIndex * 10 ) + '  id from user1) order by id asc '
end  
end  
EXEC  ( @SQLSTR )
GO

转载于:https://www.cnblogs.com/it563/archive/2008/03/28/1126778.html

猜你喜欢

转载自blog.csdn.net/weixin_30399155/article/details/94797134