Default values

You can handle these inevitable errors in many ways. In some applications, more lines of code are used to handle errors than to execute functionality. In the examples in this section, however, one of the simplest solutions, the default value, should work, as the following example shows:
avg_price = 0; /* set default for errors */
EXEC SQL SELECT avg (total_price)
      INTO :avg_price:null_flag
      FROM items;
if (null_flag < 0) /* probably no rows */
   avg_price = 0; /* set default for 0 rows */
The previous example deals with the following considerations:
  • If the query selects some non-null rows, the correct value is returned and used. This result is the expected and most frequent one.
  • If the query selects no rows, or in the much less likely event, selects only rows that have null values in the total_price column (a column that should never be null), the indicator variable is set, and the default value is assigned.
  • If any serious error occurs, the host variable is left unchanged; it contains the default value initially set. At this point in the program, the programmer sees no need to trap such errors and report them.