2004. január 28., szerda

Generate the SELECT-statement in run-time


Problem/Question/Abstract:

Generate the script for SELECT-statement

Answer:

I want to publish a small procedure that generate a SELECT-statement for data of table. This code I uses in DIM: Database Information Manager (http://www.scalabium.com/download/dbinfo.zip):

function GetSelectTable(Dataset: TTable): TStrings;
var
  i: Integer;
  str: string;
begin
  Result := TStringList.Create;
  try
    for i := 0 to DataSet.FieldCount - 1 do
    begin
      if i = 0 then
        str := 'SELECT'
      else
        str := ',';
      str := str + ' ' + DataSet.Fields[i].FieldName;
      Result.Add(str);
    end;
    Result.Add('FROM ' + DataSet.TableName)
  except
    Result.Free;
    Result := nil;
  end;
end;

Of course, you can add the ORDER BY-clause (just iterate by index fields)...

Nincsenek megjegyzések:

Megjegyzés küldése