SQL Help Needed

Associate
Joined
10 Nov 2005
Posts
650
Location
Unknown
Hi there

Just looking for some basic help in MS SQL.

Is there any way to select only the 2nd row from a table?

ie. SELECT ROW 2 * FROM table

any suggestions?

Many thanks,
Ross
 
martinweston said:
Why would ya want to do this!!!!!!

Oh, and not sure about MS SQL but if it is the same as MySQL:

SELECT * FROM `your_table` LIMIT 2, 1

Pretty much I have got a table with 3 values in it.

I need the value from the 2nd row.

I'll try that or variations now.

Many thanks,
Ross
 
Jaffa_Cake said:
Use an auto-incrementing (<-- spelling) ID field.

SELECT * FROM table WHERE id = 2

Yip would love to, however I am not able to add a field to the table as it would require testing of hundreds of other stored procedures... which I'm not prepared to do.

I'm using Transact-SQL I found out, if that is off any use to anyone.
 
OK, presumably you must have some sort of id key or something to order by?
Is it possible to use this id to get the correct row?

If you're not ORDERing this query then there is no guarantee that the database will return the correct row.
There is no concept of order in a database table unless you specifically order the query yourself.
 
Without knowing much about your data structure it sould be a bit tricky, but an SELECT TOP 2 * WHERE Whatever = 1 ORDER BY SomeField DESC; would in theory achieve what you're after, assuming the row you're after is always the last one in the result set when ordered by date, autonumber field etc.

Failing that, are you giving the results of your query to a scripting language? It'd be easy to only display the second returned row in ASP or VB for example.

*ninja*
Damn you Haircut! :)
 
Back
Top Bottom