DECLARE @batchId INT = 2 DECLARE @rowsOffset INT = 0 DECL...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

DECLARE @batchId INT = 2
DECLARE @rowsOffset INT = 0
DECLARE @batchSize INT = 50000
DECLARE @strEx NVARCHAR(256)

-- 100000 overall limit per run and cant down load result of 2nd query so

IF OBJECT_ID('tempdb.dbo.##tmpPostIDs') IS NOT NULL
  DROP TABLE ##tmpPostIDs

SELECT @rowsOffset = (@batchSize * (@batchId-1));
SELECT @strEx = N'CREATE TABLE ##tmpPostIDs ( RowId INT IDENTITY(' + CAST((@rowsOffset+1) AS VARCHAR) + N',1)'
               + N', Id INT );';
PRINT(@strEx)
EXEC (@strEx)

IF( 1=1)
BEGIN
  INSERT INTO ##tmpPostIDs (Id)
  SELECT Id FROM Posts ORDER BY ViewCount DESC OFFSET @rowsOffset ROWS FETCH NEXT @batchSize ROWS ONLY;
  
  SELECT @batchId AS BatchId, t.RowId, p.Id, p.ViewCount, p.Title, p.Body
  FROM Posts p JOIN ##tmpPostIDs t ON t.Id = p.Id
  ORDER BY t.RowId;

END;
DROP TABLE ##tmpPostIDs;

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results