There are circumstances in which program code may meet the technical requirements of SQLScript, but can still lead to undesired or nonsensical results. In such cases, it is helpful to trap the problematic states (sometimes the result of bad inputs, sometimes resulting in undesired results) and lead the SQLScript code to a clean exit, preferably one that informs the end user about what problems occurred and how to avoid them in the future.
The first step to resolving errors is to determine situations in which they may occur.
Causes of Errors
The following are the situations in which errors occur:
Missing parameters
Mismatched datatypes
Invalid or undesired values
Misleading results
Errors to Trap
Consider a procedure that is supposed to return a total number of stolen cars.
It should show all, based on Brand and Color as input parameters:
Call the procedure: are both parameters present?
- No: Error – missing parameter(s)
- Yes: Are both parameters the correct datatype?
- Not: Error – mismatched type(s)
- Yes: Do the Brand/Color values exist?
- No: Misleading output: invalid Brand or Color value means no cars exist to be stolen
- Yes: Misleading output: Brand and Color are independent valid values, but no car with that combination of values exists, so no cars exist to be stolen
Correct: Yes: PlateNumbers to the Stolen table are joined and this combination is the only one occurring among stolen cars.
- The two errors will result in an abort if left unaddressed.
- If fully executed, two misleading output states would both report a misleading total of zero stolen cars.
- Only the last case provides a meaningful total (including zero total).
In this example, the procedure should have several possible exit points:
The two errors should lead to messages to the user, indicating an unsuccessful execution due to bad inputs, and showing the required parameters/types.
The first misleading output, if joined, will report zero stolen cars. However, this is misleading because the calculation is done with nonexistent values of Brand and/or Color. This branch of the logic tree should exit without joining and inform the user that the supplied Brand and/or Color is not valid. A list of unique values may be returned to the user to show valid values.
The second misleading output, if joined, will also report zero stolen cars. However, this is because no cars exist in the Brand/Color combination. Even though every value supplied is valid, it is not meaningful to indicate a total of zero stolen cars. This branch of the logic tree should exit without joining and inform the user no such cars exist.
Only if a valid and existing combination of Brand/Color is supplied should PlateNumber values be joined against the Stolen table, and a COUNT(*) performed. Note that zero totals in this case are meaningful.

