Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Select 3 records
Message
From
12/03/2008 16:17:36
 
 
To
12/03/2008 16:06:50
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01301530
Message ID:
01301544
Views:
17
>Here is an interesting question. Is it possible to construct a sql-select statement that will return 3 adjacent records with the wanted keyfield in the middle? That is, a specified record WHERE keyfield = lnVal plus the record before and the record after. The 2 boundary records would be for the purpose of identifying the previous and next records for navigation purposes.

You might be able to brute-force it by UNIONing 3 separate SELECTs, one each for the previous, current, and next rows. Rough pseudo-code:
SELECT ;
  ... ;
  WHERE PKey = lnVal ;
  UNION ;
    SELECT TOP 1 ;
    ...
    WHERE PKey < lnVal ;
    ORDER BY ... DESC
  UNION ;
    SELECT TOP 1 ;
    ...
    WHERE PKey > lnVal ;
    ORDER BY ... ASC
   
You'd need special handling when previous or next rows don't exist, or if another user INSERTs a new record that would be the "true" previous or next record while the first user is pondering her initial result set.
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform