Databases Reference
In-Depth Information
4.3.4 Host variable declarations
Host variables used in static SQL statements are declared in a DECLARE
SECTION. For example, let us declare the static host variables needed for our
program, see Example 4-6.
Example 4-6 Host variable declarations
EXEC SQL BEGIN DECLARE SECTION;
char PID[11];
sqlint32 quantity;
char location[129];
EXEC SQL END DECLARE SECTION;
The EXEC SQL portion of the declaration is needed to indicate the beginning of
an embedded SQL statement and must be terminated by a semicolon. The
normal form is:
EXEC SQL <Standard SQL statement>;
In the INVENTORY table in the SAMPLE database, the PID and LOCATION columns are
of type VARCHAR(10) and VARCHAR(128), respectively. In the above
declaration, we have defined the size of those columns to be of one more
character to take into account the null terminator. This will ensure that returned
data values from the database do not get truncated. It is permissible to have
more than one DECLARE section in a source file, although all host variables
declared must be distinct within the source file.
After declaring the host variables, we have the option of using them as input or
output variables in an SQL statement. Example 4-7 shows the permissible usage
of host variables within an SQL statement.
Example 4-7 SQL Statement with both input and output host variables
SELECT quantity, location into :quantity, :location from inventory
where PID=:PID;
The input host variable for the above statement is PID and a value for it will be
supplied to the database by the application. The output host variables are
QUANTITY and LOCATION, and its data values will be returned from the
database to the application. In both cases, a colon (:) must precede the host
variable name.
Search WWH ::




Custom Search