AnyDAC
ContentsIndexHome
PreviousUpNext
TADCustomQuery.ExecSQL Method ()

Executes the SQL statement for the query.

Group
Links

Call ExecSQL to execute the SQL statement currently assigned to the SQL property. 

Use ExecSQL to execute queries that do not return a cursor to data (such as INSERT, UPDATE, DELETE, and CREATE TABLE). For SELECT statements and others, returning cursors, call Open instead of ExecSQL. If AnyDAC has recognized a command as returning a cursor, then "[AnyDAC][Phys]-310. Cannot execute command returning results set." will be raised on ExecSQL. This may be incorrect, for example on batch commands, like a: 

 

SELECT f1 INTO @v1 FROM myTab1 WHERE ...;
INSERT INTO myTab2 VALUES (@v1 + 1, ...)

 

In this case you should explicitly change command kind: 

 

ADQuery1.Command.CommandKind := skInsert;
ADQuery1.ExecSQL;

 

For Array DML call Execute instead of ExecSQL. 

ExecSQL prepares the statement in SQL property for execution if it has not already been prepared. To speed performance, an application should ordinarily call Prepare before calling ExecSQL for the first time. 

Use ResourceOptions.CmdExecMode to control asynchronous execution mode. And ResourceOptions.CmdExecTimeout to set maximum command execution time. After that time command execution will be canceled and exception raised. 

To cancel command execution use TADAdaptedDataSet.AbortJob

Before command execution the BeforeExecute event is fired. If server will return command execution error, then AnyDAC raises an exception. It may be analyzed in OnError event. After command execution is finished, AfterExecute event is fired.

procedure ExecSQL; overload;
// standard parameterized SQL execution
ADQuery1.SQL.Text := 'insert into mytab values (:id, :name)';
ADQuery1.Params[0].AsInteger := 100;
ADQuery1.Params[0].AsString := 'qwe';
ADQuery1.ExecSQL;

See AnyDAC\Samples\Comp Layer\TADQuery\ExecSQL\Async demo for asynchronous execution mode.

// avoid [AnyDAC][Phys]-310
ADQuery1.SQL.Clear;
ADQuery1.SQL.Add('SELECT f1 INTO @v1 FROM myTab1 WHERE ...;');
ADQuery1.SQL.Add('INSERT INTO myTab2 VALUES (@v1 + 1, ...)');
ADQuery1.Command.CommandKind := skInsert;
ADQuery1.ExecSQL;
What do you think about this topic? Send feedback!