Saturday, February 25, 2012

Returning range of records in MSSQL

Hi guys, I need to know if there is a way to select a range of records from a database. Kind of like using SELECT TOP 1000, but I need to be able to specify which records to return. So I imagine it would look like this:

SELECT TOP 2000-5000 * FROM customers WHERE groupid=2 ORDER BY FirstName DESC

Where this statement would return only records 2000 to 5000 of the returned results.Did you try reading the manual?

Not found it?

Reason: no, there is no way to do this.

Sorry.|||Well, you *could* do something like this, although I suggest you check the performance.


SELECT
*
FROM
(
SELECT TOP 3001
*
FROM
(
SELECT TOP 5000
*
FROM
customers
WHERE
groupid = 2
ORDER BY
FirstName DESC
) AS S1
ORDER BY
FirstName ASC
) AS S2
ORDER BY
FirstName DESC

Terri

No comments:

Post a Comment