Step 3: Build and Execute an SQL Statement

The third step is to build and execute an SQL statement, as shown in the following figure. The methods used to perform this step are likely to vary tremendously. The application might prompt the user to enter an SQL statement, build an SQL statement based on user input, or use a hard-coded SQL statement. For more information, see “Constructing SQL Statements” in Chapter 8, “SQL Statements.”

If the SQL statement contains parameters, the application binds them to application variables by calling SQLBindParameter for each parameter. For more information, see “Statement Parameters” in Chapter 9, “Executing Statements.”

After the SQL statement is built and any parameters are bound, the statement is executed with SQLExecDirect. If the statement will be executed multiple times, it can be prepared with SQLPrepare and executed with SQLExecute. For more information, see “Executing a Statement” in Chapter 9, “Executing Statements.”

The application might also forgo executing an SQL statement altogether and instead call a function to return a result set containing catalog information, such as the available columns or tables. For more information, see “Uses of Catalog Data” in Chapter 7, “Catalog Functions.”

The application’s next action depends on the type of SQL statement executed.

Type of SQL statement Proceed to
SELECT or catalog function Step 4a: Fetch the Results
UPDATE, DELETE, or INSERT Step 4b: Fetch the Row Count
All other SQL statements “Step 3: Build and Execute an SQL Statement” (this topic) or “Step 5: Commit the Transaction