Вы можете использовать оконные агрегатные функции.то есть COUNT(*) OVER()
пример ниже.
;WITH cte As
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY number) AS RN,
COUNT(*) OVER() AS Cnt
FROM master..spt_values
)
SELECT *
FROM cte
WHERE RN BETWEEN 101 and 200
Или- Не очень серьезное предложение, но оно избегает катушек, рабочих столов и двойной сортировки: -)
DECLARE @Spid INT = @@Spid
DECLARE @TraceID INT
DECLARE @maxfilesize BIGINT = 5
DECLARE @filepath NVARCHAR(200) = N'C:\trace_' + LEFT(NEWID(),36)
EXEC sp_trace_create @TraceID OUTPUT, 0, @filepath, @maxfilesize, NULL
exec sp_trace_setevent @TraceID, 146, 1, 1
exec sp_trace_setevent @TraceID, 146, 22, 1
exec sp_trace_setevent @TraceID, 146, 34, 1
exec sp_trace_setevent @TraceID, 146, 51, 1
exec sp_trace_setevent @TraceID, 146, 12, 1
-- filter for spid
EXEC sp_trace_setfilter @TraceID, 12, 0, 0, @Spid
-- start the trace
EXEC sp_trace_setstatus @TraceID, 1
;WITH cte AS
(
SELECT number, type, name,
ROW_NUMBER() OVER (ORDER BY number, type, name) AS RN
FROM master..spt_values
)
SELECT * FROM cte
WHERE RN BETWEEN 101 AND 200
OR RN+1 =0 /*To stop a "TOP 200" getting added to the plan*/
;WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' as sql)
SELECT ActualRows
FROM fn_trace_getinfo(@TraceID) fn
CROSS APPLY fn_trace_gettable(CAST(value AS NVARCHAR(200)), 1)
CROSS APPLY (SELECT CAST(TextData AS XML) AS xPlan) x
CROSS APPLY (SELECT T.relop.value('@ActualRows', 'INT') AS ActualRows
FROM xPlan.nodes('//sql:RelOp[@LogicalOp="Segment"]/sql:RunTimeInformation/sql:RunTimeCountersPerThread') T(relop)) ca
WHERE property = 2
AND ObjectName<>'fn_trace_getinfo' AND TextData NOT LIKE '%ThisQuery%'
-- Stop the trace
EXEC sp_trace_setstatus @TraceID, 0
-- Close and delete the trace
EXEC sp_trace_setstatus @TraceID, 2