views:

68

answers:

2

I beg you don't ask me why am I using SQL Server 6.5

There is no SELECT TOP command in SQL Server 6.5, and guess what, I need it :)

I need to perform something like

Select top 1 * from persons
where name ='Mike'
order by id desc

I've tried something with SET ROWCOUNT 1, but in that case you cannot use order by.

I end up with

Select top 1 * from persons
where id = (select max(id) from persons where name ='Mike' )

There must be better way!

Any suggestions?

Thanx!

A: 

SET ROWCOUNT 1 before your select statement, haven't tested this as I do not have mssql 6.5 (lucky I guess)

Spooks
tried that but you cannot use order by when setting rowcount :/
100r
I wonder if you could do a rowcount, and inside have a select with your order
Spooks
+2  A: 

Try selecting into a temporary table, ordered by ID, then SET ROWCOUNT 1 and select * from temporary table. (This should work for any top N with SET ROWCOUNT N, while your existing solution will only work for top 1.)

Mark Bannister
I need only top 1, so I'm fine with it. But what is more expensive, nested select or temp table?
100r
@100r, I think the correct answer to that is "it depends". The only way to be certain would be to try running both and see how they compare.
Mark Bannister