ALTER Proc [dbo].[sp_pages]
(
@Sql varchar(max) ,
@Order varchar(4000) ,
@CurrentPage int ,
@PageSize int,
@TotalCount int output
)
As
/*Andy 2012-2-28 */
Declare @Exec_sql nvarchar(max)
Declare @Exec_sql2 nvarchar(max)
Set @Exec_sql='Set @TotalCount=(Select Count(1) From ('+@Sql+') As a)'
Exec sp_executesql @Exec_sql2,N'@TotalCount int output',@TotalCount output
Set @Order=isnull(' Order by '+nullif(@Order,''),' Order By getdate()')
if @CurrentPage=1 /*经常会调用第1页,这里做特殊处理,少一层子查询*/
Set @Exec_sql='
;With CTE_Exec As
(
'+@Sql+'
)
Select Top(@pagesize) *,row_number() Over('+@Order+') As r From CTE_Exec Order By r
'
Else
Set @Exec_sql='
;With CTE_Exec As
(
Select *,row_number() Over('+@Order+') As r From ('+@Sql+') As a
)
Select * From CTE_Exec Where r Between (@CurrentPage-1)*@pagesize+1 And @CurrentPage*@pagesize Order By r
'
Exec sp_executesql @Exec_sql,N'@CurrentPage int,@PageSize int',@CurrentPage,@PageSize
Cmd.Parameters("@TotalCount").value 取不到值,因为这里有二个
Exec sp_executesql 求方法,如果把最后的Exec sp_executesql 删掉,把上面的放下来就行。