AnyDAC supports SQL command batches. Command batches allowing to execute multiple SQL commands in a single step and to process multiple result sets in a sequence.
A Command Batch is a group of SQL statements sent at one time from an application to a DBMS for execution. DBMS compiles the statements of a batch into a single execution plan. The statements in the execution plan are then executed one at a time. This minimizes network traffic and server workload. After execution, the DBMS returns to the client result sets produced by the commands.
AnyDAC allows you to process all result sets one by one, using the NextRecordSet method. To enable processing of all result sets you should set FetchOptions.AutoClose to False before executing a command. Otherwise right after reaching EOF at first cursor, it will be closed and other cursors will be discarded.
For example with SQL Server:
ADQuery1.FetchOptions.AutoClose := False;
ADQuery1.SQL.Add('select * from [Orders]');
ADQuery1.SQL.Add('select * from [Order Details]');
ADQuery1.Open; // [Orders] table rows are accessable here
ADQuery1.NextRecordSet; // [Order Details] table rows available here
An application may store each result set into a separate dataset, using TADMemTable and Data property. For example:
ADQuery1.FetchOptions.AutoClose := False; ADQuery1.SQL.Text := 'select * from orders; select * from customers'; ADQuery1.Open; ADQuery1.FetchAll; // assign orders records to ADMemTable1 ADMemTable1.Data := ADQuery1.Data; ADQuery1.NextRecordSet; ADQuery1.FetchAll; // assign customers records to ADMemTable2 ADMemTable2.Data := ADQuery1.Data;
AnyDAC automatically skips empty result sets that do not have columns and rows.
A DBMS must support command batches to execute them using AnyDAC. If a DBMS does not support batches, then you can use SQL scripting. The DBMS supporting batches are:
|
DBMS |
Syntax and Delimiters |
|
IBM DB2 |
Commands must be separated by ';'. |
|
Firebird |
Use the EXECUTE BLOCK construction. |
|
Microsoft SQL Server |
Commands may be optionally separated by ';'. |
|
MySQL |
Commands must be separated by ';'. |
|
Oracle |
Use the BEGIN END anonymous block construction. |
|
PostgreSQL v >= 9.0 |
Use the DO BEGIN END anonymous block construction. |
|
SQLite |
Commands must be separated by ';'. |
|
SQL Anywhere |
Commands may be optionally separated by ';'. |
Also, AnyDAC fully supports commands returning multiple result sets. Some examples:
Note, to execute multiple INSERT / UPDATE / DELETE commands, consider to use Array DML feature, which is far more effective for large batches.
|
What do you think about this topic? Send feedback!
|