执行上一个脚本

时间:2017-05-29 08:13:30

标签: delphi firedac

执行以下代码时,执行最后一个脚本时出错。

错误是:

“无法打开文件”c:\ FiredacDemo \ las.sql“

我无法理解为什么它会执行精确命名的脚本@fdrop_dables,@ first,@ second并且仅用于尝试执行的最后命令而不是sql文件。

procedure TForm1.btnMasterScriptClick(Sender: TObject); begin with FDScript.SQLScripts do begin Clear; with Add do begin Name := 'root'; SQL.Add('@drop_tables'); SQL.Add('@first'); SQL.Add('@second'); SQL.Add('@last'); end; with Add do begin Name := 'drop_tables'; SQL.Add('drop TABLE T1;'); SQL.Add('drop TABLE T2;'); SQL.Add('drop TABLE T3;'); SQL.Add('drop TABLE T4;'); end; with Add do begin Name := 'first'; SQL.Add('CREATE TABLE T1 (ID INTEGER NOT NULL, NAME VARCHAR(50), CREATED TIMESTAMP);'); SQL.Add('CREATE TABLE T2 (ID INTEGER NOT NULL, NAME VARCHAR(50), CREATED TIMESTAMP);'); end; with Add do begin Name := 'second'; SQL.Add('CREATE TABLE T3 (ID INTEGER NOT NULL, NAME VARCHAR(50), CREATED TIMESTAMP);'); SQL.Add('CREATE TABLE T4 (ID INTEGER NOT NULL, NAME VARCHAR(50), CREATED TIMESTAMP);'); end; with Add do begin Name := 'last'; SQL.Add(format('INSERT INTO T1 (ID, FNAME, CREATED) VALUES(%d, %s, current_timestamp);', [ 1, 'fabio', now])); SQL.Add('commit;'); end; end;

    FDScript.ValidateAll;
    FDScript.ExecuteAll;

FDScript.ValidateAll; FDScript.ExecuteAll;

编辑:这个有效!

end;

procedure TForm1.btnMasterScriptClick(Sender: TObject); begin FDScript.SQLScriptFileName := ''; FDScript.Arguments.Clear;

    with FDScript.SQLScripts do begin
      Clear;
      with Add do begin
        Name := 'root';
        SQL.Add('@drop_tables');
        SQL.Add('@first_script');
        SQL.Add('@second_script');
        SQL.Add('@third_script');
        SQL.Add('@last_script');

        SQL.Add('@');  // Added this -> see below
      end;
      with Add do begin
        Name := 'drop_tables';
        SQL.Add('drop TABLE T1;');
        SQL.Add('drop TABLE T2;');
        SQL.Add('drop TABLE T3;');
        SQL.Add('drop TABLE T4;');
      end;
      with Add do begin
        Name := 'first_script';
        SQL.Add('CREATE TABLE T1 (ID INTEGER NOT NULL, NAME VARCHAR(50), CREATED TIMESTAMP);');
        SQL.Add('CREATE TABLE T2 (ID INTEGER NOT NULL, NAME VARCHAR(50), CREATED TIMESTAMP);');
      end;
      with Add do begin
        Name := 'second_script';
        SQL.Add('CREATE TABLE T3 (ID INTEGER NOT NULL, NAME VARCHAR(50), CREATED TIMESTAMP);');
        SQL.Add('CREATE TABLE T4 (ID INTEGER NOT NULL, NAME VARCHAR(50), CREATED TIMESTAMP);');
      end;
      with Add do begin
        Name := 'third_script';
        SQL.Add(Format('INSERT INTO T1 (ID, NAME) VALUES(%d, %s);', [ 1, '''fabio''']));
      end;
      with Add do begin
        Name := 'last_script';
        SQL.Add(Format('INSERT INTO T1 (ID, NAME) VALUES(%d, %s);', [ 2, '''stefano''']));
      end;

      with Add do begin  // addes this (see above)
        Name := '';      // please note the empty '' Name
      end;               // 

    end;

    FDScript.ValidateAll;
    FDScript.ExecuteAll;

0 个答案:

没有答案