Using an Update Component's Query Property

From RAD Studio
Jump to: navigation, search

Go Up to Executing an Update Statement Index

Attention: 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 Query property of an update component provides access to the query components that implement its DeleteSQL, InsertSQL, and ModifySQL statements. In most applications, there is no need to access these query components directly: you can use the DeleteSQL, InsertSQL, and ModifySQL properties to specify the statements these queries execute, and execute them by calling the update object's Apply or ExecSQL method. There are times, however, when you may need to directly manipulate the query component. In particular, the Query property is useful when you want to supply your own values for parameters in the SQL statements rather than relying on the update object's automatic parameter binding to old and new field values.

Note: The Query property is only accessible at run time.

The Query property is indexed on a TUpdateKind value:

  • Using an index of ukModify -- accesses the query that updates existing records.
  • Using an index of ukInsert -- accesses the query that inserts new records.
  • Using an index of ukDelete -- accesses the query that deletes records.

The following example shows how to use the Query property to supply parameter values that can't be bound automatically:

procedure TForm1.BDEClientDataSet1BeforeUpdateRecord(Sender: TObject; SourceDS: TDataSet;
          DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var Applied: Boolean);
  UpdateSQL1.DataSet := DeltaDS; { required for the automatic parameter substitution }
  with UpdateSQL1.Query[UpdateKind] do
    { Make sure the query has the correct DatabaseName and SessionName }
    DatabaseName := (SourceDS as TDBDataSet).DatabaseName;
    SessionName := (SourceDS as TDBDataSet).SessionName;
    ParamByName('TimeOfUpdate').Value = Now;
  UpdateSQL1.Apply(UpdateKind); { now perform automatic substitutions and execute }
  Applied := True;
void __fastcall TForm1::BDEClientDataSet1BeforeUpdateRecord(TObject *Sender,
   TDataSet *SourceDS, TCustomClientDataSet *DeltaDS, TUpdateKind UpdateKind, bool &Applied)
  UpdateSQL1->DataSet = DeltaDS; // required for the automatic parameter substitution
  TQuery *pQuery = UpdateSQL1->Query[UpdateKind]; // access the query
  // make sure the query has the correct DatabaseName and SessionName
  TDBDataSet *pSrcDS = dynamic_cast<TDBDataSet *>(SourceDS);
  pQuery->DatabaseName = pSrcDS->DatabaseName;
  pQuery->SessionName = pSrcDS->SessionName;
  // now substitute values for custom parameters
  pQuery->ParamByName("TimeOfLastUpdate")->Value = Now();
  UpdateSQL1->Apply(UpdateKind); // now do automatic substitution and execute
  Applied = true;

See Also