Record selection advise needed

J

John

Hi

I have a winform database app that loads one record at a time. I use the
following two sqls to allow users to scroll to next and previous records;

MoveNext

SELECT TOP 1 <field list>
FROM tblClients
WHERE (ID > ?)
ORDER BY ID


Move Prev

SELECT TOP 1 <field list>
FROM tblClients
WHERE (ID < ?)
ORDER BY ID DESC

This works fine on a unique id and when records are scrolled in id order.

The client now wants the records scrolled in company type+company name
order. The problem is that niether company type nor company name is unique
and it is difficult to select next record in sequence without a unique id as
to which is the next record. How can I achieve the scrolling in company
type+company name order one record at a time?

Thanks

Regards
 
C

Cor Ligthert[MVP]

John,

In my idea do you first have to determ what is the next in those
collections,

Cor
 
A

Armin Zingler

Cor said:
John,

In my idea do you first have to determ what is the next in those
collections,

I'm not sure if it means the same as I was also about to write:
If you have

company type: 1, name: Ligthert
company type: 1, name: Ligthert

which is the first and which the second? That's what you, John, have to
determine first.


Armin
 
J

Jesús

Add the primary key or a unique key to sort order:

MoveNext:

SELECT TOP 1 <field list>
FROM tblClients
WHERE
CompanyType > @LastReadCompanyType
OR (
CompanyType = @LastReadCompanyType
AND CompanyName > @LastReadCompanyName
)
OR (
CompanyType = @LastReadCompanyType
AND CompanyName = @LastReadCompanyName
AND CompanyID > @LastReadCompanyID
)
ORDER BY
CompanyType, CompanyName, CompanyID


MovePrev:

SELECT TOP 1 <field list>
FROM tblClients
WHERE
CompanyType < @LastReadCompanyType
OR (
CompanyType = @LastReadCompanyType
AND CompanyName < @LastReadCompanyName
)
OR (
CompanyType = @LastReadCompanyType
AND CompanyName = @LastReadCompanyName
AND CompanyID < @LastReadCompanyID
)
ORDER BY
CompanyType DESC, CompanyName DESC, CompanyID DESC


Regards

Jesús López
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top