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

First, I don't think you can use < or > with the PK, you want not equal...

Second, I don't think you want to reverse the order just because you want the previous record... if you sort desc, and ask for previous, then you will actually get the same record you will get if you ask for next record.

Also, I don't think LAST name only is enough... the problem comes in if you say, give me the LastName >= Angely AND pk <> 'PKOFCURRENT'... This will work if you are on the First Angely, but if your PK is of the second Angely, the query will match the FIRST angely as easily as the third.

So, two solutions I can think of...

1. Page through last names... give the user a list of which of the records to select:

Next last name:
select top 1 *
from names
where cLastName > 'Angelly'
order by cLastName
Previous last name:
select top 1 *
from names
where cLastName < 'Angelly'
order by cLastName
2. This seems to be the most popular from what I have read. Since you will have the item number returned you can use it to build the next query.
CREATE TABLE #ordered
(item int identity primary key
,pk uniqueidentifier)

INSERT INTO #ordered (pk)
SELECT pk_name FROM name ORDER BY cLastName

SELECT o.item, n.* 
FROM names n
JOIN #ordered o ON n.pk_name = o.pk
WHERE item = @itemnumber

DROP TABLE #ordered
BOb
Previous
Reply
Map
View

Click here to load this message in the networking platform