Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 How to retrive 1st, next, previous & last record ?

Author  Topic 

NewSQLUser
Starting Member

2 Posts

Posted - 2007-04-20 : 10:18:52
Hi, I am new to using SQL. Currently, I'm using the following statemens to retrive a specific record from my MS Access DB via VB.net.

SELECT * FROM table_name WHERE Field_Name = Criteria

Can someone please tell me, after selecting this record, If I want to go to the FIRST, or NEXT or PREVIOUS of the record just retrived or the LAST record. Can someone please tell me how can write the SQL statment to achieve this ?

Regards

Kristen
Test

22859 Posts

Posted - 2007-04-20 : 10:46:28
Next:

SELECT TOP 1 * FROM table_name WHERE Field_Name > Criteria ORDER BY SomeKey(s)

Previous:

SELECT TOP 1 * FROM table_name WHERE Field_Name < Criteria ORDER BY SomeKey(s) DESC

First / Last just leave out the WHERE but keep the ORDER BY

Its a bit more complicated if your CRITERIA is on multiple fields:

Next:

WHERE MyColumn1 > @Parameter1
OR (MyColumn1 = @Parameter1 AND MyColumn2 > @Parameter2)

and the reverse for Previous.

Kristen
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-04-22 : 21:24:52
Please explain what you are doing. It is very likley that there is a better way to accomplish what you want without finding the next,previous,last,etc. record.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-04-23 : 01:45:45
I don't know about the O/P, but on our Maintenance Pages we provide Next / Previous so that the User can "walk" through the records one-by-one making changes.

Kristen
Go to Top of Page
   

- Advertisement -