Preparing a query

From InterBase
Jump to: navigation, search

Go Up to Working with Queries


Preparing a query is an optional step that precedes query execution. Preparing a query submits the SQL statement and its parameters, if any, for parsing, resource allocation, and optimization. The server, too, may allocate resources for the query. These operations improve query performance, making your application faster, especially when working with updatable queries.

An application can prepare a query by calling the Prepare method. If you do not prepare a query before executing it, then Delphi automatically prepares it for you each time you call Open or ExecSQL. Even though Delphi prepares queries for you, it is better programming practice to prepare a query explicitly. That way your code is self-documenting, and your intentions are clear. For example:

CustomerQuery.Close;
if not (CustomerQuery.Prepared) then
  CustomerQuery.Prepare;
CustomerQuery.Open;

This example checks the query component’s Prepared property to determine if a query is already prepared. Prepared is a Boolean value that is True if a query is already prepared. If the query is not already prepared, the example calls the Prepare method before calling Open.

Advance To: