Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Previous/Next Row
Message
 
 
To
29/01/2004 10:55:42
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
00871822
Message ID:
00871825
Views:
10
Dan,

The both queries may return incorrect row because pk_name should only be compared if cLastName is the same. Try
-- Next
select top 1 *
from names
where cLastName > 'Angelly' OR
	(cLastName = 'Angelly' and
      pk_name > '3E4BB82C-C7DF-4BFF-974E-BC47EDC7264D')
order by cLastName, pk_name

-- Previous 
select top 1 *
from names
where cLastName < 'Angelly' OR
	(cLastName = 'Angelly' and
      pk_name < '3E4BB82C-C7DF-4BFF-974E-BC47EDC7264D')
order by cLastName desc, pk_name desc
>We want to implement a feature that will allow our front end to request the previous or next row (based on a specified sort order). We only want to "fetch" one record at a time however. So let's say we have a table similar to this:
>
>
>PK_Name                               LastName    FirstName
>------------------------------------  ----------- ----------
>86405F3D-F928-4EEE-8460-98ED9FA82379  Anderson    Grant
>6A3A4BEF-2F21-4BE6-BE9A-B6298B0BD7B9  Anderson    Chris
>68528A47-EE1A-40AA-87F7-ED6BDAF27770  Anderson    Terry
>644D5299-29E3-4F53-A8ED-F6DF42C2BB86  Anderson    Edwin
>B6FB293A-D650-4894-B5EF-EC7E34D55064  Andriani    Drew
>C7C7E3F6-93CE-4F1A-AEF2-FA3ACCE33CD7  Andritzky   Christian
>3E4BB82C-C7DF-4BFF-974E-BC47EDC7264D  Angelly     Clay
>3EFA8C5E-B06C-4107-BD0B-D6D11542B213  Antonitis   Nancy
>FD571681-DE6B-47B3-AD88-F4C00A7B71A2  Anwari      Aman
>A4A7F32C-2F72-4314-85AC-995DFB7B6194  Anzziani    John
>20C5FBE3-278D-485D-AF20-9E1BD6136860  Aquino      Jun
>781B8B34-8E95-45DA-B850-CF6DCC044131  Arastoozad  John
>
>
>I have the row for Clay Angelly (PK 3E4BB82C-C7DF-4BFF-974E-BC47EDC7264D). Now I want to send a SELECT statement to the server to get either the PREVIOUS or NEXT row based on a sort order of last name.
>
>It appears that:
>
>
>select top 1 *
>from names
>where cLastName >= 'Angelly' and
>      pk_name > '3E4BB82C-C7DF-4BFF-974E-BC47EDC7264D'
>order by cLastName, pk_name
>
>
>Will give me the NEXT row. However,
>
>
>select top 1 *
>from names
>where cLastName <= 'Angelly' and
>      pk_name < '3E4BB82C-C7DF-4BFF-974E-BC47EDC7264D'
>order by cLastName desc, pk_name desc
>
>
>Will not always give me the PREVOIUS row. It does sometimes, but not always.
>
>Is there a good way to do this?
--sb--
Previous
Reply
Map
View

Click here to load this message in the networking platform