> Ever tried accessing the columns from a SELECT * statement, on a table which
> has had the order of the columns rearranged? (Which is totally fine by me,
> BTW)
> 
> Ever had SELECT * code go bang when an obsolete column is removed from a
> table, eg:
...
> When your application attempts to access 'phone' based on it's position
> (fields[3]) things get ugly.

If for any reason you absolutely have to use 'SELECT *' or a polymorphic view
and your record set to iterate over will be large then use an array of offset rather
than the string comparison of FieldByName so that you can reliably use Fields[n]
instead.

EG

Type
   TSQLField = (ftID,ftNAME,ftAGE);
const
   SQLFieldNames :array[TSQLField] of String = ('ID','Name','AGE');
var
  SQLFieldOffsets :array[TSQLField] of TField;
  ft :TSQLField;
begin
  // Q is a TQuery in this example
  for ft := low(ft) to high(ft) do SQLFieldOffsets[ft] := 
Q.Fields.FindField(SQLFieldNames[ft]);

  while not Q.EOF do begin
     Showmessage('Name is :'+SQLFieldOffsets[ftNAME].AsString);
    Q.next;
  end;
end;

The effort is only worthwhile for large iteration counts but the string searching is a 
sizable
overhead.

There's one legitimate reason for 'select *' and that is where the object reading from 
the
current dataset record has been inherited from and will read additional fields not 
anticipated
by the original writer (although there are mechanisms for dealing better with this 
too)...

--
Aaron Scott-Boddendijk
Jump Productions
(07) 838-3371 Voice
(07) 838-3372 Fax


---------------------------------------------------------------------------
  New Zealand Delphi Users group - Database List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to