I'm using a TDataSet where the CommandText property is set to an SQL query. I have also made the following function which creates part of an SQL query based on the fields of TDataSet. It is however incomplete. As you can see I still need to get the name of the table that a TField is from. How do I achieve this?
function GetDataSetFieldsMSSQL(Dataset: TDataSet): String;
var
  I, L: Integer;
  TableName: String;
begin
  Result := '';
  L := Dataset.Fields.Count;
  if (L > 0) then
  begin
    TableName := ... // Name of the table for the Dataset.Fields[0] field.
    Result := '[' + TableName + '].[' + Dataset.Fields[0].FieldName + ']';
    I := 1;
    while (I < L) do
    begin
      TableName := ... // Name of the table for the Dataset.Fields[I] field.
      Result := Result + ',[' + TableName + '].[' + Dataset.Fields[I].FieldName + ']';
      Inc(I);
    end;
  end;
end;