简体   繁体   中英

Delphi table Join

I've been tasked with patching a Delphi software to work with a new database(mssql) structure. In the previous database(mssql), all the columns being read were in the same table. In the new version the file_name and class_name are in a different table as the quantity values. I believe I can fix this by writing a Join for these two database tables. Issue is, I'm not familiar with Delphi!
The current code is below. How do I get these two databases to join into one?

dataTable=style001 dataTable1=style

THANKS!!!

  dataTable.Active := True;
  datatable.Open;
  dataTable1.Active := True;
  datatable1.Open;

  while not datatable1.Eof do
  begin
    Application.ProcessMessages;

    file_name := trim(datatable1.FieldByName('code').asString) + '.jpg';
    class_name := trim(datatable1.FieldByName('category').asString);

    if not FileExists(picfolder + file_name) then
    begin
      dataTable1.next;
      continue;
    end;

    instock := datatable.FieldByName('onhand').asString;
    TryStrToInt(instock, instock_num);

    quantity := datatable.FieldByName('onorder').asString;
    TryStrToInt(quantity, num);

    num := instock_num - num;

    is_active := ( num > 10 );


    for i := 0 to file_count-1 do
    begin
      if is_active = active_class[file_class[i]] then
      if SameText(files[i],file_name) then
      begin // use TStringList instead
        if SameText(class_name, classes[file_class[i]]) then // same class
        begin
          file_ok[i] := true;
          class_ok[file_class[i]] := true;
        end;
        break;
      end;
    end;

    if class_name <> '' then class_name := class_name + '\';
    dest := picfolder + active_path[is_active] + class_name;
    deldest := picfolder + active_path[not is_active] + class_name;

    {$I-}

    if FileExists(deldest + file_name) then
      DeleteFile(PChar(deldest + file_name ));

    if not FileExists(dest + file_name) then
    begin
      ForceDirectories(dest);
      CopyFile(PChar(picfolder + file_name ), PChar(dest + file_name ), true );
    end;

    dataTable1.next;
  end;

  for i := 0 to file_count-1 do
    if not file_ok[i] then
    begin // delete wrong file if empty
//      ShowMessage('problem: ' + picfolder + active_path[active_class[file_class[i]]] + classes[file_class[i]] + '\' + files[i]);
      DeleteFile(picfolder + active_path[active_class[file_class[i]]] + classes[file_class[i]] + '\' + files[i]);
    end;
  for i := 0 to class_count-1 do
    if not class_ok[i] then
    begin // delete old class if empty
//      ShowMessage('problem: ' + picfolder + active_path[active_class[i]] + classes[i] + '\' );
      RemoveDir(picfolder + active_path[active_class[i]] + classes[i] + '\' );
    end;


  beep;
  dataTable.Active := False;
  dataTable1.Active := False;

The JOIN is inside the SQL request, not the consuming loop code you are showing here.

You have to use a TQuery instead of two TTable , and write a SQL select for joining the two tables.

You have to modify the SQL request, add the two tables in the FROM clause of the SELECT, and a JOINture in the WHERE clause. See this article about JOIN .

Use a TDataSource and connect its DataSet property to one of the tables. Then set the MasterSource property of the other table to that datasource. Click on the ellipsis button of the MasterFields property and select the connecting fields.

Assuming that the style fields are unique, when you navigate through the first table the second one will follow automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM