RAD Studio |  |

Using an Update Component's Query Property
See Also
Collapse All

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 runtime.
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 shows how to use the Query property to supply parameter values that can't be bound automatically:
Copy Code

    procedure TForm1.BDEClientDataSet1BeforeUpdateRecord(Sender: TObject; SourceDS: TDataSet;
              DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var Applied: Boolean);
    begin
      UpdateSQL1.DataSet := DeltaDS; { required for the automatic parameter substitution }
      with UpdateSQL1.Query[UpdateKind] do
      begin
        { Make sure the query has the correct DatabaseName and SessionName }
        DatabaseName := (SourceDS as TDBDataSet).DatabaseName;
        SessionName := (SourceDS as TDBDataSet).SessionName;
        ParamByName('TimeOfUpdate').Value = Now;
      end;
      UpdateSQL1.Apply(UpdateKind); { now perform automatic substitutions and execute }
      Applied := True;
    end;

Copy Code

    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

Calling the Apply Method

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!