Using TQuery (Procedure)
Go Up to How To Perform Database Procedures
TQuery is a query-type dataset that encapsulates an SQL statement and enables applications to access the resulting records.
To use TQuery
- Choose File > New > Other. The New Items dialog appears.
- In the New Items dialog, select Delphi Projects and double-click VCL Forms Application. The Windows Designer displays.
- Associate the dataset with database and session connections.
- Create heterogeneous queries.
- Obtain an editable result set.
- Update read-only result sets.
To associate a dataset with database and session connections
- From the BDE category of the Tool Palette, drag a TDatabase component to the form.
- Drag a TSession component to the form.
- Set the DatabaseName property of the TDatabase component to associate a BDE-enabled dataset with a database. For the TDatabase component, database name is the value of the DatabaseName property of the database component.
- Specify a BDE alias as the value of DatabaseName if you want to use an implicit database component and the database has a BDE alias.
Note: A BDE alias represents a database plus configuration information for that database. The configuration information associated with an alias differs by database type (Oracle, Sybase, InterBase, Paradox, dBASE, and so on).
- In the Object Inspector, set the DatabaseName to specify the directory where the database tables are located if you want to use an implicit database component for a Paradox or dBASE database.
- Use the default session to control all database connections in your application.
- Set the SessionName property of the TSession component to associate your dataset with an explicitly created session component.
Note: Whether you use the default session or explicitly specify a session using the SessionName property, you can access the session associated with a dataset by reading the DBSession property. If you use a session component, the SessionName property of a dataset must match the SessionName property for the database component with which the dataset is associated.
To create mixed queries
- Define separate BDE aliases for each database accessed in the query using the BDE Administration tool or the SQL explorer.
- Leave the DatabaseName property of the TQuery component blank. The names of the databases used will be specified in the SQL statement.
- Set the SQL property to the SQL statement you want to execute.
- Precede each table name in the statement with the BDE alias for the database of the table, enclosed in colons. This whole reference is then enclosed in quotation marks.
- Set the Params property to any parameters for the query.
- Write a Prepare method to prepare the query for execution prior to executing it for the first time.
- Write an Open or ExecSQL method depending on the type of query you are executing.
- Use a TDatabase component as an alternative to using a BDE alias to specify the database in a mixed query.
- Configure the TDatabase to the database, set the TDatabase. DatabaseName to an unique value, and use that value in the SQL statement instead of a BDE alias name.
To obtain an editable result set
- Set RequestLive property of the TQuery component to True.
- If the query contains linked fields, treat the result set as a read-only result set, and update it.
If an application requests a live result set, but the SELECT statement syntax does not allow it, the BDE returns either a read-only result set for queries made against Paradox or dBASE, or an error code for SQL queries made against a remote server.
To update read-only result sets
- If all updates are applied to a single database table, indicate the underlying table to update in an OnGetTableName event handler.
- Set the query's UpdateObject property to the TUpdateSQL object you are using to have more control over applying updates.
- Set the DeleteSQL, InsertSQL, and ModifySQL properties of the update object to the SQL statements that perform the appropriate updates for your query's data.
If you are using the BDE to cache updates, you must use an update object.