下面是存储过程的创建,用的时候调用就行了
/****** Object: StoredProcedure [dbo].[sq_paging] Script Date: 09/10/2014 09:57:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE proc [dbo].[Yc_PageSelect] @tbName varchar(255), --表名 @tbFields varchar(1000), --返回字段 @PageSize int, --页尺寸 @PageIndex int, --页码 @strWhere varchar(1000), --查询条件 @StrOrder varchar(255), --排序条件 @Key varchar(20) --排序字段 as declare @strSql varchar(5000) --主语句 declare @strSqlCount nvarchar(500)--查询记录总数主语句 --------------总记录数--------------- if @strWhere !=‘‘ begin set @strSqlCount=‘Select COUNT(*) as TotalCout from ‘ + @tbName + ‘ where ‘+ @strWhere end else begin set @strSqlCount=‘Select COUNT(*) as TotalCout from ‘ + @tbName end --------------分页------------ if @PageIndex <= 1 begin set @strSql=‘select top ‘+str(@PageSize)+‘ ‘+@tbFields+‘from ‘ + @tbName + ‘ where ‘ + @strWhere +‘ ‘ + @strOrder end else begin set @strSql=‘select top ‘+str(@PageSize)+‘ ‘+@tbFields+‘from ‘ + @tbName + ‘ where ‘ + @strWhere +‘ AND ‘+@Key +‘ > (select MAX(‘+@Key +‘) from ( select top ‘+str((@PageIndex-1)*@PageSize)+‘ ‘+@Key +‘ from ‘ + @tbName + ‘ where ‘ + @strWhere +‘ ‘+@strOrder +‘ )a) ‘ + @strOrder end exec (@strSqlCount) exec(@strSql) GO