<p>Task: implement paging of database records suitable for different RDBMS. Method should work for mainstream engines - MSSQL2000+, Oracle, MySql, etc.</p>

<p>Please don't post RDBMS specific solutions, I know how to implement this for most of the modern database engines. I'm looking for the universal solution. Only temporary tables based solutions come to my mind at the moment.</p>

<p><strong>EDIT:</strong><br>
I'm looking for SQL solution, not 3rd party library.</p>

[{'answer_id': 61754, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>SubSonic can do this for you if you if you can tolerate Open Source...\n<a href="http://subsonicproject.com/querying/webcast-using-paging/" rel="nofollow noreferrer">http://subsonicproject.com/querying/webcast-using-paging/</a></p>\n\n<p>Other than that I know NHib does as well</p>\n'}, {'answer_id': 61757, 'author': 'jodonnell', 'author_id': 4223, 'author_profile': 'https://Stackoverflow.com/users/4223', 'pm_score': 0, 'selected': False, 'text': "<p>JPA lets you do it with the Query class:</p>\n\n<pre><code>Query q = ...;\nq.setFirstResult (0);\nq.setMaxResults (10);\n</code></pre>\n\n<p>gives you the first 10 results in the result set.</p>\n\n<p>If you want a DBMS independent raw SQL solution, I'm afraid you're out of luck.  All the vendors do it differently.</p>\n"}, {'answer_id': 61939, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 2, 'selected': False, 'text': '<p>The most natural and efficient way to do paging is using the LIMIT/OFFSET (TOP in Sybase world) construct. A DBindependent way would have to know which engine it\'s running on and apply the proper SQL construct.</p>\n\n<p>At least, that\'s the way I\'ve seen it done in DB independent libraries\' code. You can abstract away the paging logic once you get the data from the engine with the specific query. </p>\n\n<p>If you really are looking for a single, one SQL sentence solution, could you show what you have in mind? Like the SQL for the temp table solution. That would probably get you more relevant suggestions.</p>\n\n<p>EDIT: </p>\n\n<p>I wanted to see what were you thinking because I couldn\'t see a way to do it with temp tables and not use a engine specific construct. You used specific constructs in the example. I still don\'t see a way to implement paging in the database with only (implemented) standard SQL. You could bring the whole table in standard SQL and page in the application, but that is obviously stupid.</p>\n\n<p>So the question would now be more like "Is there a way to implement paging without using LIMIT/OFFSET or equivalent?" and I guess that the answer is "Sanely, no." You could try using cursors but you\'ll fall prey to database specific sentences/behavior there as well.</p>\n\n<p>A wacko (read stupid) idea that just occurred to me would be to add a page column to the table, say create table test (id int, name varchar, phone varchar, page int) and then you can get page 1 with select * from table where page = 1. But that means having to add code to maintain that column, which, again could only be done by either bringing the whole database or using database specific constructs. That besides having to add a different column per each possible ordering and many other flaws.</p>\n\n<p>I can\'t provide proof, but I really think you just can\'t do it sanely.</p>\n'}, {'answer_id': 61958, 'author': 'Troels Arvin', 'author_id': 4462, 'author_profile': 'https://Stackoverflow.com/users/4462', 'pm_score': 1, 'selected': False, 'text': '<p>Proceed as usual:\n<br />\nStart by implementing it according to the standard. And then handle the corner cases, i.e. the DBMSes which don\'t implement the standard. How to handle the corner cases depends on your development environment.</p>\n\n<p>You are looking for a "universal" approach. The most universal way to paginate is through the use of cursors, but cursor-based pagination don\'t fit very well with a non-stateful environment like a web application.</p>\n\n<p>I\'ve written about the standard and the implementations (including cursors) here:\n<a href="http://troels.arvin.dk/db/rdbms/#select-limit-offset" rel="nofollow noreferrer">http://troels.arvin.dk/db/rdbms/#select-limit-offset</a></p>\n'}, {'answer_id': 61985, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 0, 'selected': False, 'text': "<p>@Vinko Vrsalovic,</p>\n\n<p>as I wrote in question I know how to do it in most DBs. I what to find universal solution or get a proof that it doesn't exist.</p>\n\n<p>Here is one stupid solution based on temporary table. It's obviously bad, so no need to comment on it. </p>\n\n<pre><code>N - upper bound\nM - lower bound\n\ncreate #temp (Id int identity, originalId int)\n\ninsert into #temp(originalId)\nselect top N KeyColumn from MyTable\nwhere ...\n\nselect MyTable.* from MyTable\njoin #temp t on t.originalId = MyTable.KeyColumn\nwhere Id between M and M\norder by Id asc\n\ndrop #temp\n</code></pre>\n"}, {'answer_id': 126470, 'author': 'user20358', 'author_id': 20358, 'author_profile': 'https://Stackoverflow.com/users/20358', 'pm_score': 4, 'selected': True, 'text': "<p>There would have been a universal solution if SQL specifications had included paging as a standard. The requirement for any RDBMS language to be called an RDBMS language does not include paging support as well. </p>\n\n<p>Many database products support SQL with proprietary extensions to the standard language. Some of them support paging like MySQL with the limit clause, Rowid with Oracle; each handled differently. Other DBMS's will need to add a field called rowid or something like that.</p>\n\n<p>I dont think you can have a universal solution (anyone is free to prove me wrong here;open to debate) unless it is built into the database system itself or unless there is a company say ABC that uses Oracle, MySQL, SQL Server and they decide to have all the various database systems provide their own implementation of paging by their database developers providing a universal interface for the code that uses it.  </p>\n"}]
['https://Stackoverflow.com/questions/61750', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1196/']