Retrieve table name from field object (TField)

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
DmitriNL
Posts: 15
Joined: Fri 26 Nov 2010 15:47

Retrieve table name from field object (TField)

Post by DmitriNL » Thu 14 Jun 2018 09:07

Hi,

I am wondering if it is possible to retrieve the used table name from a processed query. In particular it would be nice to retrieve table names and columns from a dataset after a query has been processed.

For example I have the following query:

Code: Select all

SELECT table1.id, table2.name, table2.value 
FROM table1, table2 
WHERE table table2.id = table1.id
I tried getting the table name from each field by looping all fields after the query is processed. I found 'Origin' but this variable seems always empty. It would be nice to know which column belong to which table. In this case it would be easier to write data back to the specific tables in the database. I'm using a Microsoft SQL database.

Thank you in advance for your response.

Stellar
Devart Team
Posts: 496
Joined: Tue 03 Oct 2017 11:00

Re: Retrieve table name from field object (TField)

Post by Stellar » Fri 15 Jun 2018 13:31

To get meta information about TUniQuery dataset field, you can use the GetFieldDesc method of TUniQuery. As an argument this method can accept:
- the TField field instance
- a field name as a string value
- a field ordinal number in dataset as an integer value

In UniDAC, the TFieldDesc class is developed for internal use, so its description is missing in the documentation and can be changed at any time.

For example, obtaining the table name, field name for TUniQuery dataset field:

Code: Select all

uses
  SqlClassesUni;

procedure TForm.Button1Click(Sender: TObject);
var
  i: Integer;
  FieldDesc: TSqlFieldDesc;
begin
  UniQuery1.SQL.Text := 'SELECT table1.id, table2.name, table2.value' +
                        'FROM table1, table2' +
                        'WHERE table table2.id = table1.id';
  UniQuery1.Open;

  for i := 0 to UniQuery1.Fields.Count - 1 do begin
    FieldDesc := TSqlFieldDesc(UniQuery1.GetFieldDesc(UniQuery1.Fields[i]));

    Memo1.Lines.Add(FieldDesc.TableInfo.TableName + '.' + FieldDesc.ActualName + '(' + FieldDesc.Name + ')');
  end;
end;

Post Reply