

CREATE PROCEDURE dbo.spAll_ReturnRows
(
@SQL nVARCHAR(4000),
@Page int,
@RecsPerPage int,
@ID VARCHAR(255),
@Sort VARCHAR(255),
@count int output
)
AS

DECLARE @Str nVARCHAR(4000)

SET @Str='select TOP '+cast(@RecsPerPage AS VARCHAR(20))+' * FROM ('+@SQL+') T where T.'+@ID+' NOT IN 
(select TOP '+cast((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM ('+@SQL+') T9 ORDER BY '+@Sort+') ORDER BY '+@Sort

PRINT @Str

EXEC sp_ExecuteSql @Str


set @count=@@ROWCOUNT
GO
