zoukankan      html  css  js  c++  java
  • FireDAC 下的批量 SQL 命令执行

    一、{逐条插入}
    
    procedure TForm1.Button1Click(Sender: TObject);
    const
      strInsert = 'INSERT INTO MyTable(Name, Age) VALUES(:name, :age)';
    begin
    //  FDQuery1.FetchOptions.AutoClose := True; //默认值
      FDQuery1.ExecSQL(strInsert, ['A', 1]);
      FDQuery1.ExecSQL(strInsert, ['B', 2]);
      FDQuery1.ExecSQL(strInsert, ['C', 3]);
      FDQuery1.ExecSQL(strInsert, ['D', 4]);
    
      FDQuery1.Open('SELECT * FROM MyTable');
    end;
    二、{一次行插入}
    procedure TForm1.Button2Click(Sender: TObject);
    const
      strInsert = 'INSERT INTO MyTable(Name, Age) VALUES("%s", %d)';
    var
      LStr: string;
    begin
      LStr := '';
      LStr := LStr + Format(strInsert, ['AA', 11]) + ';';
      LStr := LStr + Format(strInsert, ['BB', 22]) + ';';
      LStr := LStr + Format(strInsert, ['CC', 33]) + ';';
      LStr := LStr + Format(strInsert, ['DD', 44]) + ';';
      LStr := LStr + 'SELECT * FROM MyTable';
    
      FDQuery1.ExecSQL(LStr);
      FDQuery1.Open();
    end;
    
    
    三、{使用 NextRecordSet 方法提取并执行所有命令}
    procedure TForm1.Button3Click(Sender: TObject);
    const
      strInsert = 'INSERT INTO MyTable(Name, Age) VALUES("%s", %d);';
    begin
      FDQuery1.FetchOptions.AutoClose := False; //按说这个是必须要设置的, 但测试时不设置也可以
      FDQuery1.SQL.Clear;
      FDQuery1.SQL.Add(Format(strInsert, ['AAA', 111]));
      FDQuery1.SQL.Add(Format(strInsert, ['BBB', 222]));
      FDQuery1.SQL.Add(Format(strInsert, ['CCC', 333]));
      FDQuery1.SQL.Add(Format(strInsert, ['DDD', 444]));
    
      FDQuery1.SQL.Add('SELECT * FROM MyTable');
    
      FDQuery1.Execute();
      FDQuery1.NextRecordSet;
    end;
    
    
    四、{使用 DML 数组参数}
    procedure TForm1.Button4Click(Sender: TObject);
    const
      strInsert = 'INSERT INTO MyTable(Name, Age) VALUES(:name, :age);';
    begin
      FDQuery1.FetchOptions.AutoClose := False; //
    
      FDQuery1.SQL.Text := strInsert;
      FDQuery1.Params.ArraySize := 4; //准备把上面的语句执行 4 次
    
      {分别设置 4 次的参数}
      FDQuery1.Params[0].AsStrings[0] := 'AAAA';
      FDQuery1.Params[1].AsIntegers[0] := 1111;
    
      FDQuery1.Params[0].AsStrings[1] := 'BBBB';
      FDQuery1.Params[1].AsIntegers[1] := 2222;
    
      FDQuery1.Params[0].AsStrings[2] := 'CCCC';
      FDQuery1.Params[1].AsIntegers[2] := 3333;
    
      FDQuery1.Params[0].AsStrings[3] := 'DDDD';
      FDQuery1.Params[1].AsIntegers[3] := 4444;
    
      FDQuery1.Execute(4, 0); //从 1 条开始执行 4 次
    
      FDQuery1.SQL.Add('SELECT * FROM MyTable');
      FDQuery1.NextRecordSet;
    end;
    
    
    五、{使用 FireDAC 扩展的 SQL Script(TFDScript), 它还能直接调用文件中的 SQL 指令}

    CREATE TABLE BOOKS(ID INTEGER PRIMARY KEY, NAME TEXT);
    INSERT INTO BOOKS(ID, NAME) VALUES(1, 'Delphi 2009 handbook');
    INSERT INTO BOOKS(ID, NAME) VALUES(2, 'Delphi XE2入門');
    INSERT INTO BOOKS(ID, NAME) VALUES(3, 'Delph');
    执行文件中描述的SQL
    
    FDScript1.ExecuteFile('C:datasample.sql');
    使用脚本参数
    TFDScript的ExecuteFile方法将脚本参数作为第二个参数。
    脚本参数是一个字符串数组。
    
    FDScript1.ExecuteFile(文件名,脚本参数);
    SQL语句中的“&职位编号”被替换为参数。
    
    例子
    C:数据 sample.sql
    
    CREATE TABLE &1(ID INTEGER PRIMARY KEY, NAME TEXT);
    INSERT INTO &1(ID, NAME) VALUES(1, '&2');
    INSERT INTO &1(ID, NAME) VALUES(2, '&3');
    INSERT INTO &1(ID, NAME) VALUES(3, '&4');
    执行文件中描述的SQL
    
    FDScript1.ExecuteFile('C:datasample.sql',
      [ 'BOOKS',
        'Delphi 2009 handbook―Delphi最新',
        'Delphi XE2入門',
        'Delphi']);
    捕捉错误
    如果执行SQL脚本时发生错误,则会引发OnError事件。
    
    procedure TForm1.FDScript1Error(ASender: TObject;
      const AInitiator: IFDStanObject; var AException: Exception);
    begin
     
      ShowMessage(AException.Message);
    end;
    获取发生的错误数
    您可以获取TotalErrors属性中发生的错误数。
    
    FDConnection1.StartTransaction;
    try
      FDScript1.ExecuteFile('C:datasample.sql');
    finally
      if FDScript1.TotalErrors > 0 then
        FDConnection1.Rollback
      else
        FDConnection1.Commit;
    end;
    显示SQL脚本执行的进度
    使用TFDGUIxScriptDialog显示SQL脚本执行的进度。
    
    TFDGUIxScriptDialog
    
    将TFDGUIxScriptDialog组件放置在窗体上,并设置TFDScript组件的ScriptDialog属性。
    
    FDScript1.ScriptDialog := FDGUIxScriptDialog1;
    
    
    
    {For example, to execute a script file, use the following code snippet: }
    
    with FDScript1 do begin
      SQLScriptFileName := 'c:create.sql';
      ValidateAll;
      ExecuteAll;
    end;
    {To execute a script in a memory, use the following: }
    
    with FDScript1 do begin
      SQLScripts.Clear;
      SQLScripts.Add;
      with SQLScripts[0].SQL do begin
        Add('INSERT INTO Brands VALUES (1, ''Audi'')');
        Add('INSERT INTO Brands VALUES (2, ''BMW'')');
      end;
      ValidateAll;
      ExecuteAll;
    end;
    {Also, there are some other methods that simplify the SQL script execution. You can control many other script execution aspects as from a Delphi code by using ScriptOptions as using corresponding script control commands. 
    
    The script can also call other scripts, such as a subroutine, through the @ <script>, @@ <script>, START <script>, or INPUT <script> commands. 
    In that case, <script> is either the item name from the SQLScripts collection, or the external file name.
    For example, the 'root' script executes the 'first' and 'second' subscripts:
    } with FDScript1.SQLScripts do begin Clear; with Add do begin Name := 'root'; SQL.Add('@first'); // explicitly call 'first' script SQL.Add('@second'); // explicitly call 'second' script end; with Add do begin Name := 'first'; SQL.Add('create table t1 (...);'); SQL.Add('create table t2 (...);'); end; with Add do begin Name := 'second'; SQL.Add('create procedure p1 (...);'); SQL.Add('create procedure p2 (...);'); end; end; FDScript1.ValidateAll; FDScript1.ExecuteAll;
    {
    A SQL script can be executed as in full with a subscripts, using ExecuteAll method,
    as in step-by-step mode using the ExecuteStep method.
    The last method is useful for the GUI applications, allowing executing adhoc queries.
    The next command will be extracted and executed from the Position position in the script. To abort the script execution, call the AbortJob method.

    }

    SQL脚本是一组独立的SQL、执行控制和日志命令。SQL脚本对于后端维护任务非常有用,比如后端SQL对象的创建、删除、升级、初始数据加载等等。

    许多dbms允许在一个TFDQuery中执行多个SQL命令。ExecSQL调用作为一个批处理,但有限制。这些是SQL脚本和SQL命令批处理之间的区别:

    该脚本允许在一个脚本中使用所有可能的SQL命令。批处理可能有限制,这取决于DBMS。例如,Oracle匿名PL/SQL块不能包含DDL命令。

    该脚本可以划分为多个事务。批处理必须在单个事务中执行。

    该脚本允许使用非sql和自定义命令。批处理只包括DBMS可以理解的命令。

    脚本可以分为下标。批处理可以将存储过程作为分离的代码块调用。

    脚本执行完全由客户机控制。批处理执行仅由DBMS控制。

    与批处理执行不同,脚本执行可以被记录。

    与批处理执行不同,脚本提供执行进度反馈。

    与标准实用程序相比,TFDScript有许多优点,比如能够完全集成到FireDAC应用程序中,并通过自定义脚本命令扩展命令集。TFDScript组件知道一些行业标准的SQL脚本语法,包括:

     

    Oracle SQL*Plus;
    Microsoft ISQL/OSQL;
    MySQL mysql.exe/mysqldump.exe;
    Firebird/InterBase ISQL.



    感谢参考 https://www.cnblogs.com/del/p/3758082.html
     
     
     
  • 相关阅读:
    Thrift安装编译指南
    Linux磁盘与文件系统管理
    你懂得C,所以C++也不在话下
    加速NFV(网络功能虚拟化)数据面:SR-IOV和DPDK[原文意译]
    十张图看懂SDN与NFV的区别与联系?
    Lambda表达式用法
    论文写作+gnuplot制图
    私钥、公钥、数字签名和数字证书
    synchronized和Lock的异同
    介绍HTTP协议的传输过程
  • 原文地址:https://www.cnblogs.com/usegear/p/13018970.html
Copyright © 2011-2022 走看看