Sqlserver 通用分页

通用分页写法:

1 create PROCEDURE [dbo].[up_Basic_Person_getListByPage]
2 @tablename sysname,
3 @primarykey varchar(20),
4 @pageSize INT=10,
5 @currentPage INT=0,
6 @sqltemplate varchar(4000) =''
7 AS
8 set @sqltemplate='select top ({pageSize}) * from {tablename} where {primarykey} not in(select top ({pageSize}*({currentPage})) {primarykey} from {tablename})';
9 set @sqltemplate=replace(@sqltemplate,'{pageSize}',@pageSize);
10 set @sqltemplate=replace(@sqltemplate,'{currentPage}',@currentPage);
11 set @sqltemplate=replace(@sqltemplate,'{tablename}',@tablename);
12 set @sqltemplate=replace(@sqltemplate,'{primarykey}',@primarykey);
13 --print @sqltemplate
14 exec (@sqltemplate)
15 go

posted @ 2011-05-04 11:02  KT野人  阅读(134)  评论(0编辑  收藏  举报