Executing an Update Statement Index

From RAD Studio
Jump to: navigation, search

Go Up to Using BDE Index

Note: The Borland Database Engine (BDE) has been deprecated, so it will not be enhanced. For instance, BDE will never have Unicode support. You should not undertake new development with BDE. Consider migrating your existing database applications from BDE to dbExpress.

The ExecSQL method for an update component manually applies updates for the current record. Unlike the Apply method, ExecSQL does not bind parameters in the SQL statement before executing it. The ExecSQL method is most often called from within a handler for the OnUpdateRecord event (when using the BDE) or the BeforeUpdateRecord event (when using a client dataset).

Because ExecSQL does not bind parameter values, it is used primarily when the update object's SQL statements do not include parameters. You can use Apply instead, even when there are no parameters, but ExecSQL is more efficient because it does not check for parameters.

If the SQL statements include parameters, you can still call ExecSQL, but only after explicitly binding parameters. If you are using the BDE to cache updates, you can explicitly bind parameters by setting the update object DataSet property and then calling its SetParams method. When using a client dataset to cache updates, you must supply parameters to the underlying query object maintained by TUpdateSQL. For information on how to do this, see Using an Update Component's Query Property.

Warning: If you use the dataset UpdateObject property to associate the dataset and the update object, ExecSQL is called automatically. In that case, do not call ExecSQL in an OnUpdateRecord or BeforeUpdateRecord event handler, as this will result in a second attempt to apply the current record update.

The OnUpdateRecord and BeforeUpdateRecord event handlers indicate the type of update that needs to be applied with an UpdateKind parameter of type TUpdateKind. You must pass this parameter to the ExecSQL method to indicate which update SQL statement to use. The following code illustrates this using a BeforeUpdateRecord event handler:

procedure TForm1.BDEClientDataSet1BeforeUpdateRecord(Sender: TObject; SourceDS: TDataSet;
          DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var Applied: Boolean);
  with UpdateSQL1 do
    DatabaseName := (SourceDS as TDBDataSet).DatabaseName;
    SessionName := (SourceDS as TDBDataSet).SessionName;
    Applied := True;
void __fastcall TForm1::BDEClientDataSet1BeforeUpdateRecord(TObject *Sender,
   TDataSet *SourceDS, TCustomClientDataSet *DeltaDS, TUpdateKind UpdateKind, bool &Applied)
  TDBDataSet *pSrcDS = dynamic_cast<TDBDataSet *>(SourceDS);
  UpdateSQL1->DatabaseName = pSrcDS->DatabaseName;
  UpdateSQL1->SessionName = pSrcDS->SessionName;
  Applied = true;

If an exception is raised during the execution of the update program, execution continues in the OnUpdateError event, if it is defined.

See Also