USEGEAR

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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:\data\sample.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:\data\sample.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:\data\sample.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

 

 
 
 

posted on   USEGEAR  阅读(1004)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示