Executing a query at runtime
From InterBase
Go Up to Executing a query
To execute a query at runtime, use one of the following methods:
Open
executes a query that returns a result set, such as with theSELECT
statement.ExecSQL
executes a query that does not return a result set, such as with theINSERT
,UPDATE
, orDELETE
statements.
Note:
If you do not know at design time whether a query will return a result set at runtime, code both types of query execution statements in a try...except block. Put a call to the
If you do not know at design time whether a query will return a result set at runtime, code both types of query execution statements in a try...except block. Put a call to the
Open
method in the try clause. This allows you to suppress the error message that would occur due to using an activate method not applicable to the type of SQL statement used. Check the type of exception that occurs. If it is other than an ENoResult
exception, the exception occurred for another reason and must be processed. This works because an action query will be executed when the query is activated with the Open
method, but an exception occurs in addition to that.try IBQuery2.Open; except on E: Exception do if not (E is ENoResultSet) then raise; end;
Executing a query that returns a result set
To execute a query that returns a result set (a query that uses a SELECT
statement), follow these steps:
- 1. Call
Close
to ensure that the query is not already open. If a query is already open you cannot open it again without first closing it. Closing a query and reopening it fetches a new version of data from the server. - 2. Call
Open
to execute the query.
For example:
IBQuery.Close; IBQuery.Open; { query returns a result set }
For information on navigating within a result set, see Disabling bi-directional cursors. For information on editing and updating a result set, see Working with result sets.
Executing a query without a result set
To execute a query that does not return a result set (a query that has a SQL statement such as INSERT
, UPDATE
, or DELETE
), call ExecSQL
to execute the query.
For example:
IBQuery.ExecSQL; { query does not return a result set }