Unanswered: Extract Records (Rows) With In A Particular Range
Is there any way in sql server to get records with in a particular range. Lets say that we got 100 records satisfying a query, I want only first 20 records or records in the range 40 - 60. Kindly suggest me how to accomplish this in sqlserver and if possible provide a code snippet regarding this. Thanks in advance.
You can do this without a temporary table. The technique is called paging and if you search dbforums you will find many different techniques that can be used. None of the are elegant, but most are functional and reliable.
My preferred method requires a specific column for sorting. Basically, the procedure accepts a StartingValue and a RecordCount value as parameters. The query then selects records from your table that are greater than your StartingValue, while a subquery returns the number of records that have sortvalues less than or equal to their own sort value, thus providing an ordinal ID. The outer query then filters only the records where the ordinal ID is <= the requested RecordCount.
Provide some DDL for your table if you need specific coding assistance.
If it's not practically useful, then it's practically useless.