as begin declare @sql nvarchar(max); set @Sql='select * from (select (row_number() over('+@sort+' by '+@pageBy+')-1)/'+convert(varchar(20),@pageSize)+' as rowId, * from '+@fromTable+') a where rowId='+convert (varchar(20),@pageIndex)+''; print @Sql; exec sp_executesql @sql; end