Sunday, February 19, 2012

How to find a record by number

What would be the best way in SQL to query a table to return a single record by number?
Let's say I want record number 49 and my primary key is a text field so a numeric key is out of the question.I think I figured it out unless anyone has a better way.

SELECT TOP 1 * FROM (SELECT TOP 49 * FROM [Table] ORDER BY [Key] ASC) AS Whatever ORDER BY [Key] DESC|||That looks like a bad idea. If you pass any number bigger than the max number of records in the table, you always get the last record.

any ideas?|||which row did you want, the 49th? by which column?

and yes, if there are fewer than 49 rows, it would seem appropriate (at least, it seems so to me) to choose the last one

what database are you using? this forum is for non-specific (i.e. standard) sql

you may want to consider the Access or SQL Server forums, if TOP works in your database

No comments:

Post a Comment