Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a table like this.

EmpCode
101
102
103
104


Suppose a user want to extract any record like 4th row or 2nd row then how to write a query using select statement which gives the exact result without using Where clause.

Is there any way through which we can find the exact record.lets take an example of array then In array if we want to get 3rd record then we can directly use ECode[3] and we get 103 as result.I want to use the same without using any cursor value value and no where clause also.
Posted
Comments
manognya kota 25-Jan-12 7:53am    
Why do you want to do it?
Amir Mahfoozi 25-Jan-12 8:55am    
No

 
Share this answer
 
Comments
fjdiewornncalwe 25-Jan-12 10:01am    
Looks like the answer the OP is looking for. +5
manognya kota 26-Jan-12 23:28pm    
Thank you Marcus :)
Today! I found the same issue as mentioned by the OP, and after GOOGLING, I landed on this SOLVED POST at CP, but shocked and terrified to see the answered link not working... :(

anyway a bit later after bit more GOOGLING , found an answer to my query,

and thought of sharing it here, just for the sake of other naive's like me for future.


Well. Let comes to the point. if a user wants to search 2nd row of a table without using WHERE clause.

the following chunk comes into the play. :)

SQL
SELECT TOP 1 T.* FROM
(SELECT TOP 2 * FROM TableName ORDER BY ID ASC) AS T
ORDER BY T.ID DESC;


The logic is pretty simple, Inner Query gets the nth number of rows i.e. 2 top rows in this case, and than outer query descended that result and get the top of that. :)

Hope it will help. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900