MSSQL翻页存储过程

Create PROCEDURE [dbo].[ShowPage]
@tblName   varchar(255),       -- 表名
@strGetFields varchar(1000) = '*',  -- 需要返回的列
@strOrder varchar(255)='',      -- 排序的字段名
@startRowIndex   int = 0,          -- 取的第一行在结果有的序号(从第几行开始取数据)
@maximumRows  int = 10,           -- 每页显多少行(每次取多少行)
@strWhere  varchar(8000) = ''  -- 查询条件 (注意: 不要加 where)
AS

declare @strSQL   nvarchar(4000)
declare @output   int
if @strWhere !=''
  set @strWhere=' where ' +@strWhere

set @strSQL='Select * FROM (Select ROW_NUMBER() OVER (order by '+@strOrder+' desc) AS pos,'+@strGetFields+' FROM ['+@tblName+']'+@strWhere+') AS sp where pos>'+str(@startRowIndex)+' and pos<'+str(@startRowIndex+@maximumRows+1)   -- BETWEEN '+str(@PageIndex*@PageSize+1)+' AND '+str((@PageIndex+1)*@PageSize)

exec (@strSQL)

 
 
set @strSQL='Select @output=count(*) FROM ['+@tblName+']'+@strWhere

execute sp_executesql  @strSQL,N'@output int output',@output output
 
return @output
 

原文地址:https://www.cnblogs.com/zsuxiong/p/2530746.html