SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO CREATE PROCEDURE [dbo].[GetPagingStr] @PRESQL VARCHAR(8000), --前部分语句,SELECT内容 @SUFSQL VARCHAR(8000), --后部分语句,从FROM开始 @SQL VARCHAR(8000) OUTPUT, --完整分页语句 @PageSize int, @PageNum int, @RecordCount int OUTPUT WITH ENCRYPTION AS IF @PageSize>0 AND @PageNum>0 --需要分页 BEGIN --执行SQL SELECT @SQL=@PRESQL + @SUFSQL + ') as PageTable where row_num between (' + CONVERT(VARCHAR(8),@PageNum) + '-1)*' + CONVERT(VARCHAR(8),@PageSize) + '+1 and ' + CONVERT(VARCHAR(8),@PageSize) + '*' + CONVERT(VARCHAR(8),@PageNum) END ELSE BEGIN--不需要分页 SELECT @SQL=@PRESQL + @SUFSQL + ') as PageTable' END --记录总数 DECLARE @COUSQL NVARCHAR(1000)--总条数SQL SELECT @COUSQL='SELECT @RecordCount=(select count(*) ' + @SUFSQL + ')' execute SP_EXECUTESQL @COUSQL,N'@RecordCount int output',@RecordCount OUT GO