Skip to content
Advertisement

SQL select single row number x only

For processing SQL data in VBA I want to use a loop (For…Next) that reads line by line from a SQL database. As records were deleted the ID column has gaps. The table looks for example like this:

Having four lines I would like to run

of course a “For each” would do good, too.

Advertisement

Answer

If you want 4 rows, then use order by and top – or a fetch clause:

Or:

You can then iterate through the resultset in your application.

If, on the other hand, you want the 4th row only, then just change the fetch clause:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement