Executing Queries That Don't Return a Result Set

From RAD Studio
Jump to: navigation, search

Go Up to Using Query-type Datasets


When a query returns a set of records (such as a SELECT query), you execute the query the same way you populate any dataset with records: by setting Active to True or calling the Open method.

However, often SQL commands do not return any records. Such commands include statements that use Data Definition Language (DDL) or Data Manipulation Language (DML) statements other than SELECT statements (For example, INSERT, DELETE, UPDATE, CREATE INDEX, and ALTER TABLE commands do not return any records).

For all query-type datasets, you can execute a query that does not return a result set by calling ExecSQL:

CustomerQuery.ExecSQL;  { query does not return a result set }
CustomerQuery->ExecSQL(); // Does not return a result set

Tip: If you are executing the query multiple times, it is a good idea to set the Prepared property to True.

Although the query does not return any records, you may want to know the number of records it affected (for example, the number of records deleted by a DELETE query). The RowsAffected property gives the number of affected records after a call to ExecSQL.

Tip: When you do not know at design time whether the query returns a result set (for example, if the user supplies the query dynamically at run time), you can code both types of query execution statements in a try...except block. Put a call to the Open method in the try clause. An action query is executed when the query is activated with the Open method, but an exception occurs in addition to that. Check the exception, and suppress it if it merely indicates the lack of a result set. (For example, TQuery indicates this by an ENoResultSet exception.)

See Also