首页 > 代码库 > 【SQL Server】Sql分页(自用)

【SQL Server】Sql分页(自用)

下面是存储过程的创建,用的时候调用就行了

/****** Object: StoredProcedure [dbo].[sq_paging] Script Date: 09/10/2014 09:57:54 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE proc [dbo].[Yc_PageSelect]@tbName varchar(255), --表名@tbFields varchar(1000), --返回字段@PageSize int, --页尺寸@PageIndex int, --页码@strWhere varchar(1000), --查询条件@StrOrder varchar(255), --排序条件@Key varchar(20) --排序字段asdeclare @strSql varchar(5000) --主语句declare @strSqlCount nvarchar(500)--查询记录总数主语句--------------总记录数---------------if @strWhere !=‘‘beginset @strSqlCount=Select COUNT(*) as TotalCout from  + @tbName +  where + @strWhereendelsebeginset @strSqlCount=Select COUNT(*) as TotalCout from  + @tbNameend--------------分页------------if @PageIndex <= 1beginset@strSql=select top +str(@PageSize)+ +@tbFields+from  + @tbName+  where  + @strWhere +  + @strOrderendelsebeginset @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) + @strOrderendexec (@strSqlCount)exec(@strSql)GO

 

【SQL Server】Sql分页(自用)