View Code
我自己现在在用的,也做个记录。CREATE procedure [dbo].[paging]
(@pagesize int, --每页显示多少条记录
@pageindex int, --显示第几页
@fromTable varchar(1000), --查询表格,包括连表条件
@strwhere varchar(1000), --查询条件
@strorderby varchar(64), --排序条件,不带Order关键字
@Fields varchar(2000), --查询字段,多个字段时用','隔开
@id varchar(30) --主键字段
)
as
declare @order varchar(10)
declare @strsql varchar(1000)
create table #tb
(id int identity(1,1),
nid int
)
begin
declare @PageLowerBound varchar(5)
declare @PageUpperBound varchar(5)
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set @strsql='insert into #tb(nid) select '+@id+' from '+@fromTable+' where '+@strwhere+' order by '+@strorderby;
exec(@strsql)
set @strsql='select count('+@id+') from '+@fromTable+' where '+@strwhere
set @strsql=@strsql+' select '+@Fields+' from '+@fromTable+',#tb t where '+@id+' = t.nid and '+' t.id > '+@PageLowerBound+' and t.id <= '+@PageUpperBound+' order by t.id'
set @strsql=@strsql+' drop table #tb'
end
exec (@strsql)