在MSSQL中利用存储过程进行分页查询

/***************************************************************************************
功能:
 分页查询数据源

过程名:Pagination_comm

参数: intCurrentPage  当前要查询的页码
 intPageSize 每页显示的记录数
 strTableName 要查询的数据源表名
 strPrimaryKey  该表中对应的主关键(或聚集索引)字段
 strOrder  记录的排序

      Created By HuangQing at 2005-7-15
****************************************************************************************/
CREATE PROCEDURE pagination_comm
(
 @intCurrentPage int,     --页码 
 @intPageSize int,          --每页容纳的记录数
 @strTableName nvarchar (100), -- 表名
 @strPrimaryKey nvarchar (100),  -- 索引字段名
 @intOrder smallint    --排序 0--升序;1--降序
)
AS

Declare @Str nVARCHAR(4000)
Declare @s_Order varchar(5)
Declare @strOrder char(8)

if (@intOrder=0)
   Set @strOrder ='Asc'
if (@intOrder=1)
   Set @strOrder='Desc'

Set @str = 'select Top ' + Cast(@intPageSize as varchar(20)) + '  * From  '+ @strTableName + ' where ('+ @strPrimaryKey +' not in (Select top '+ Cast(@intPageSize*@intCurrentPage as varchar (20)) +'  '+ @strPrimaryKey +'  from '+ @strTableName + ' order by '+ @strPrimaryKey +'  '+ @strOrder +' )) order by '+ @strPrimaryKey +' '+ @strOrder

print @str


EXEC sp_ExecuteSql @Str
GO

当然,我这里查询的时候,其结果是查询出全部字段的内容,在实现工作可根据需要再进行修改。

posted @ 2005-11-10 13:54  小小点  阅读(266)  评论(0编辑  收藏  举报