MSSQLSERVER数据库- 分页存储过程示例
牛兄的分页存储过程.
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROCEDURE [dbo].[proc_SplitPage] @tblName varchar(255), -- 表名 @strFields varchar(1000) = '*', -- 需要返回的列,默认* @strOrder varchar(255)='', -- 排序的字段名,必填 @strOrderType varchar(10)='ASC', -- 排序的方式,默认ASC @PageSize int = 10, -- 页尺寸,默认10 @PageIndex int = 1, -- 页码,默认1 @strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where) AS Begin declare @strSQL varchar(5000) if @strWhere !='' set @strWhere=' where '+@strWhere set @strSQL= 'SELECT * FROM ('+ 'SELECT ROW_NUMBER() OVER (ORDER BY '+@strOrder+' '+@strOrderType+') AS pos,'+@strFields+' '+ 'FROM '+@tblName+' '+@strWhere+ ') AS sp WHERE pos BETWEEN '+str((@PageIndex-1)*@PageSize+1)+' AND '+str(@PageIndex*@PageSize) exec (@strSQL); --执行SQL语句 End
调用示例一
EXEC [proc_SplitPage]
@tblName = N'(project p inner join projectca ca on p.caid=ca.id)',
@strFields = N'p.title,ca.caname',
@strOrder = N'p.id',
@strOrderType = N'desc',
@PageSize = 12,
@PageIndex = 1,
@strWhere = N''
调用示例二
EXEC [proc_SplitPage]
@tblName = N'meeting',
@strFields = N'*',
@strOrder = N'id',
@strOrderType = N'desc',
@PageSize = 5,
@PageIndex = 4,
@strWhere = N''