Sql-简单分页

create proc proc_searchuser
(
@username varchar(12),
@page int=1,
@pagesize int=3,
@totalcount int output

)
as
declare @totalsql nvarchar(512)
declare @sql varchar(4000)

if(ISNULL(@username,'')<>'')
begin
set @totalsql='select @totalcount=COUNT(*) from users where username like ''%'+@username+'%'''
end
else
begin
set @totalsql='select @totalcount=COUNT(*) from users'
end
exec sp_executesql @totalsql,N'@totalcount int output',@totalcount output
-------------分页--------------
if @page<=0 set @page=1

set @sql='select * from (select ROW_NUMBER() over(order by userid)rowNO,* from users where username like ''%'+@username+'%'')U
where U.rowNo BETWEEN '+str((@page-1)*@pagesize+1)+' AND ' +str(@page*@pagesize)


exec (@sql)
go

posted @ 2013-08-06 22:32  zhangchun  阅读(328)  评论(0编辑  收藏  举报