Databases Reference
In-Depth Information
Example 4-18 Using indicator values to check nullability
EXEC SQL SELECT quantity, location INTO :quantity INDICATOR :ind1,
:location INDICATOR :ind2 from inventory where PID=:PID;
The INDICATOR keyword and the indicator host variable are added after each
host variable to be checked. We need to ensure that the indicator host variables
ind1 and ind2 are declared in the DECLARE section of the program. As the
indicator variable is of SMALLINT data type, the C data type can be declared to
be short. An indicator host variable with a value of -1 is null, any other value is
considered not-NULL. The INDICATOR keyword is optional. Example 4-19
shows the same SQL statement without the INDICATOR keyword.
Example 4-19 Using indicator without INDICATOR keyword
EXEC SQL SELECT quantity, location INTO :quantity :ind1, :location :ind2 FROM
inventory where PID=:PID;
Note: There is no comma between a host variable and its indicator.
4.3.14 The WHENEVER Statement
Within an application, we can specify how to handle certain SQL conditions such
as if an SQL error, warning, or a not found condition (SQLCODE of +100) is
returned from the database.
There are three main types of the WHENEVER clause that can be in an
embedded SQL statement:
EXEC SQL WHENEVER NOT FOUND < action >
EXEC SQL WHENEVER SQLERROR < action >
EXEC SQL WHENEVER SQLWARNING < action >
The possible actions are CONTINUE, GOTO < label >, or GO TO < label >.
In Example 4-17 on page 165, we added WHENEVER clauses into our
query_product() function. If a NOT FOUND condition occurs, we will print out the
appropriate error message. Otherwise, we print a more generic error message.
4.3.15 Preparing SQL statements
We have been working with embedded static SQL statements in the
add_product() and query_product() functions in our sample inventory program.
However, for the inventory operation where a column is to be updated, we need
to use embedded dynamic SQL and dynamically prepare the SQL statement.
Search WWH ::




Custom Search