uses Windows, DBTables,SysUtils;

Type
  PTable = ^TTable;

const
  db_dep = 0;
  db_kaf = 1;
  db_std = 2;
  db_chk = 3;
  db_KSC = 4;
  db_stdchk = 5;
  _C : char = '''';

var
 i : Integer;
 Table  : Array [0..6] of TTable;
 AppDir : String;

 Procedure Filter(Tbl:PTable; S:String);
 begin
    tbl.Filtered := false;
    tbl.Filter   := S;
    tbl.Filtered := true;
    if not tbl.eof then
    Tbl.First;
 end;

 Procedure OpenBD(Tbl: Array of PTable);
 var i : Integer;
 begin
   for i := 0 to high(tbl) do
   begin
    Tbl[i].Open;
    Tbl[i].First;
   end;

 end;

 Procedure CloseBD(Tbl: Array of PTable);
 var i : Integer;
 begin
   for i := 0 to high(tbl) do
   begin
    Tbl[i].Filtered :=false;
    Tbl[i].First;
    Tbl[i].Close;
   end;
 end;

Procedure CheckCreateField( Table_ID: integer; Field: string);
var
i : Integer;
begin
  with table[ Table_ID ] do
  begin
      Open;
    for i := 0 to FieldDefs.count -1 do
               if FieldDefs.Items[i].Name = Field then
                  begin
                    Close;
                    exit;
                  end;
      Close;
  end;
    with TQuery.Create(nil) do
        begin
          Close;
           SQL.Clear;
           SQL.Add('alter table "'+table[ Table_ID ].TableName+'" add '+Field+' char(32) ;');
          ExecSQL;
          Close;
          Free;
        end;
end;

Procedure CheckCreateTable(TableName, addFields: string);
begin
  if FileExists(TableName) then exit;
    with TQuery.Create(nil) do
    begin
     SQL.Clear;
     SQL.Add('create table "'+TableName+'" ( '+addFields+' );');
     ExecSQL;
     Close;
    Free;
    end;
end;



begin

for i := 0 to high( Table ) do
  Table[i] := TTable.Create(nil);
  Appdir   := ExtractFilepath( ParamStr(0) ) + '\WorkDir\';

  table[ db_dep    ].TableName := AppDir + 'dep1.DB';
  table[ db_kaf    ].TableName := AppDir + 'kaf2.DB';
  table[ db_std    ].TableName := AppDir + 'standard3.DB';
  table[ db_chk    ].TableName := AppDir + 'check4.DB';
  table[ db_KSC    ].TableName := AppDir + 'KSC5.DB';
  table[ db_stdchk ].TableName := AppDir + 'std_check_log.db';

  CheckCreateField(db_std,'Standard_RepName');
  CheckCreateField(db_KSC,'KSC_Standard_Weight');

  CheckCreateTable(table[ db_stdchk ].TableName ,
  'stdchk_id AUTOINC,'+
  'stdchk_dep_id int,'+
  'stdchk_kaf_id int,'+
  'stdchk_std_id int,'+
  'stdchk_chk_id int,'+
  'stdchk_std_date date,'+
  'stdchk_std_reg_id char(64),'+
  'stdchk_std_errortext char(255),'+
  'stdchk_boss_name char(255)');
  MessageBox(0, PChar('Патч выполнен'),'=)',0);
end.