Getting Range In DB2

DB2 is the database bane of my online existence. It doesn’t support even the most basic of SQL commands, let alone handy dandy functions such as LIMIT and the seriously-limited-but-none-the-less-useful TOP found in MySQL and an assortment of other SQL databases. So how am I supposed to select a range of recordsin DB2? With nested selects. Its ugly, I don’t think its too efficient, and so I don’t have to go hunting for something like this ever again, its here:

SELECT *

FROM table_name t1

WHERE startrow <= (SELECT COUNT(*)
FROM table_name t2
WHERE t1.key > t2.key)

AND endrow > (SELECT COUNT(*)
FROM table_name t2
WHERE t1.key > t2.key)

ORDER BY table_key

If anybody knows of a better way, please share.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*