Subject | Re: Record count alternative |
---|---|
Author | Adam |
Post date | 2006-02-06T23:18:13Z |
> I know that record counts are a slow process in FB and are to be(sometimes)
> avoided. I am doing an ordering app where the results of a
> complex query are displayed. The client wants it to automaticallyorder
> the item if only one result is returned, which is reasonable.However
> to determine if there is only one result I think I need to eitherdo a
>back again.
> a) Query.RecordCount
> b) Move to the next record, if EOF than only one returned, move
>I am guessing that you don't want to splice a not exists clause into
> Is there a better way?
the query itself in some sort of case statement. I would go for b,
but use a common function to do it, and name it so it is obvious what
you are doing. From memory, you are using delphi, otherwise you can
translate it to whatever.
function OnlyContainsOneRecord(qry : TDataset) : Boolean;
var
function IsFirstRecord : Boolean;
begin
qry.Prior;
Result := qry.BOF;
if not Result then qry.Next;
end;
function IsLastRecord : Boolean;
begin
qry.Next;
Result := qry.EOF;
if not Result then qry.Prior;
end;
begin
Result := (IsFirstRecord and IsLastRecord);
end;
Now your code is perfectly intuitive;
if OnlyContainsOneRecord(Query) then OrderProduct;
Adam