Subject Re: [firebird-support] last record of query
Author Svein Erling Tysvaer
There is a fourth way, add another field to your query telling you
whether it is the last field or not:

Select <whatever>,
case when exists(select 1 from TableA a2
where a2.PK > a.PK)
then 1 else 0
end as EofField
from TableA a
order by PK

Admittedly, this adds some overhead, but it is a fourth option...

Set

Woody wrote:
> From: "Sergio H. Gonzalez" <shg_sistemas@...>
>> Delphi 6, Interbase Express.
>>
>> Hello
>> Is there way to know if I'm on the last record of a query? I need to do
>> something special (in a report) on the last record. I can't check for Eof,
>> because It's false until the next skip on the table.
>>
>> So far I'm using
>>
>> function LastRecord(t:TIBQuery): boolean;
>> begin
>> t.Next;
>> result := t.Eof;
>> t.Prior;
>> end;
>>
>
> If you are at the EOF, then Next does not change the position of the record
> pointer. It stays on the last record. However, in your code, you aren't
> testing to see if you are at the EOF before performing the Prior.
>
> There are 3 ways to do this (IMO):
>
> 1. The way you are doing it but I would add this test:
> if not result then
> t.Prior;
>
> 2. Get a record count before starting to print and check the printed count
> against that.
>
> 3. Do a separate query to get the ID of the last record using a reverse
> ordering (DESC) and compare that.
>
> HTH
> Woody (TMW)