IDalCommand.Execute

Syntax

Execute: Integer;

Description

The Execute method executes the command at the database server and returns the number of processed records.

Comments

The method executes the SQL query specified in the IDalCommand.SQL property. If the command is used to extract data, it is recommended to use the CreateCursor method instead of the Execute method.

If the SQL query changes data (adds or deletes records, or updates the existing records), the method returns the number of processed records. If parameters are used in the query, their actual values are substituted with those in the IDalCommand.Params collection. If the command is used for batch insert of multiple sets of parameter values, all sets in the range from zero to CurrentParamsRow are processed on executing the method. After executing the Execute method, the CurrentParamsRow property is set to zero.

If an error occurs on executing the SQL query, an exception is thrown.

Example

Executing the example requires that the repository contains a database with the DB identifier and a table with the MyTable identifier.

Add links to the Dal, Db, and Metabase system assemblies.

Sub UserProc;
Var
    Mb : IMetabase;
    DB_ORCL8 : IDatabaseInstance;
    Command : IDalCommand;
    Params : IDalCommandParams;
    sql : String;
    paramArraySize, i, j : Integer;
Begin
    MB := MetabaseClass.Active;
    DB_ORCL8 := MB.ItemById("DB").Open(NullAs IDatabaseInstance;
    sql := "insert into MyTable (field,field1) values(:1,:2)";
    Command := DB_ORCL8.Connection.CreateCommand(sql);
    Command.Type := DalCommandType.Text;
    Command.Parse;
    Command.Prepare;
    Params := Command.Params;
    Params.Item(0).DataType := DbDataType.String;
    Params.Item(1).DataType := DbDataType.Float;
    paramArraySize := 3;
    Command.MaxParamsRows := paramArraySize;
    j := 1;
    For i := 1 To 10 Do
        Params.Item(0).Value := "№ "+i.ToString;
        Params.Item(1).Value := i + 0.1;
        If (j < paramArraySize) Then
            Command.NextParamsRow;
            j := j+1;
            Else
                Command.Execute;
                j:= 1;
        End If;
    End For;
    If j > 1 Then
        Command.ExecuteWithoutLast;
    End If;
End Sub UserProc;

After executing the example ten records are inserted into the table.

See also:

IDalCommand