What is the best way to fetch records batch-wise from SQL Server

14,341

Solution 1

If you can't use OFFSET-FETCH in SQL Server 2012 and assuming the table has a primary key or column(s) that allow you to uniquely identify a row, lets call it UniqueKey, then in 2005 upwards you could use ROW_NUMBER like this...

SELECT UniqueKey, col2, col3 
FROM 
(
  SELECT UniqueKey, col2, col3, ROW_NUMBER() OVER (ORDER BY UniqueKey) AS RowNum 
  FROM YourTable
) sub
WHERE sub.RowNum BETWEEN @startRow AND @endRow

Solution 2

If you use MSSQL 2012 try OFFSET-FETCH clause. It is the best solution!

Example: SELECT … ORDER BY orderid OFFSET 25 ROWS fetches only the next 25 rows.

It means this query will return from 25 to 50 records. The ORDER BY clause is mandatory, so if you don't want to use order, use ORDER BY (SELECT NULL)

Share:
14,341
Amol M Kulkarni
Author by

Amol M Kulkarni

Fell in ♥ with Codes... Especially JS ;-) Currently Working on broader web platform (targeting all possible devices and browsers)You'll find me contributing, authoring many projects across the internet. I also ♥ helping others in solving technical problem & I believe this is a way to stay up-to-date and give something back to the community from where I have learnt and still learning :) Worked on: ADO.NET, AJAX, ASP.NET, C, C#, C++, Corel draw, CouchDB, CouchBase, Crystal Reports, CSS, DHTML, Dreamweaver, EJS, Express Framework Node.js, Go-lang, HTML, Infragistics, Jade, Java, Java Applets, JavaScript, jQuery, JSP, Membase, Memcache, Microsoft SQL Server, MongoDB with Python and Node.js, MongoDB(DBA), MongoDB (4.2-Basics), MongoDB New Features and Tools in 4.2, MongoDB Performance Tuning, MS-DOS, Netbeans, No SQL, Node.js, Oracle, OAuth, PageMaker, PhotoShop, Servlets, Socket.io, SwishMax, Tally, VBA, VBScript, Visual Basic, Visual InterDev, Windows Mobile C#, XHTML, XML, XQuery, XSLT, Python, PHP, Kafka, Storm, 0MQ (ZMQ), Redis, Hadoop Favorite Quotation: "Only He Who Can See The Invisible Can Do The Impossible." profile for Amol M Kulkarni on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/2185406.png

Updated on June 18, 2022

Comments

  • Amol M Kulkarni
    Amol M Kulkarni almost 2 years

    Scenario: we are fetching rows from SQL Server to C#.Net console application and doing action on the retrieved data from SQL Server through stored procedure; after the action is performed the new data is stored into the MongoDB using C#-MongoDB-Driver.

    Issue: There are billions of rows. My stored procedure contains query as follows:

    select * from table_name
    

    To work out some batch-wise logic there is no identity column nor any date columns or such.

    Information: As of now the application is fetching the records upto 3500 - 5000 records and storing into MongoDB and then it throws an error which is as follows:

    System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.

    Question: Can anyone suggest me some logic to work out for batch-wise read/fetch from SQL Server?

  • davmos
    davmos almost 11 years
    The question tags would suggest they are using SQL Server 2008
  • Amol M Kulkarni
    Amol M Kulkarni almost 11 years
    Yes best solution you had proposed, provided we are on MS SQL 2012. But we are on MS SQL 2008 & on 2008 R2.. Thanks for your time..
  • Zohar Peled
    Zohar Peled almost 4 years
    "you could use...ROW_NUMBER() OVER (ORDER BY (SELECT 0))...to try and preserve the natural order that the records are stored in." - Well, that's just wrong. There is no "natural order" - it's a misconception.
  • Zohar Peled
    Zohar Peled almost 4 years
    There's this old article in a website called 4GuysFromRolla about efficient paging in SQL Server 2000. I think it might still work in 2008. Worth giving it a shot.
  • davmos
    davmos almost 4 years
    @ZoharPeled thank-you very much! Over the years I had doubted the validity of that statement, but never got round to confirming for sure. So I have now removed it. Thanks also for the useful links, I will have a look at those :)