IBM DB2 Manual page 127

Table of Contents

Advertisement

|
|
Host variables and host expressions are case sensitive.
A complex expression is an array element or Java expression that evaluates to a
single value. A complex expression in an SQLJ clause must be surrounded by
parentheses.
The following examples demonstrate how to use host expressions.
Example: Declaring a Java identifier and using it in a SELECT statement:
In this example, the statement that begins with #sql has the same function as a
SELECT statement in other languages. This statement assigns the last name of the
employee with employee number 000010 to Java identifier empname.
String empname;
...
#sql [ctxt]
{SELECT LASTNAME INTO :empname FROM EMPLOYEE WHERE EMPNO='000010'};
Example: Declaring a Java identifier and using it in a stored procedure call:
In this example, the statement that begins with #sql has the same function as an
SQL CALL statement in other languages. This statement uses Java identifier empno
as an input parameter to stored procedure A. The value IN, which precedes empno,
specifies that empno is an input parameter. For a parameter in a CALL statement, IN
is the default. The explicit or default qualifier that indicates how the parameter is
used (IN, OUT, or INOUT) must match the corresponding value in the parameter
definition that you specified in the CREATE PROCEDURE statement for the stored
procedure.
String empno = "0000010";
...
#sql [ctxt] {CALL A (:IN empno)};
Example: Using a complex expression as a host identifier:
This example uses complex expression (((int)yearsEmployed++/5)*500) as a host
expression.
#sql [ctxt] {UPDATE EMPLOYEE
SET BONUS=:(((int)yearsEmployed++/5)*500) WHERE EMPNO=:empID};
SQLJ performs the following actions when it processes a complex host expression:
v Evaluates each of the host expressions in the statement, from left to right, before
assigning their respective values to the database.
v Evaluates side effects, such as operations with postfix operators, according to
normal Java rules. All host expressions are fully evaluated before any of their
values are passed to DB2.
v Uses Java rules for rounding and truncation.
Therefore, if the value of yearsEmployed is 6 before the UPDATE statement is
executed, the value that is assigned to column BONUS by the UPDATE statement
is ((int)6/5)*500, or 500. After 500 is assigned to BONUS, the value of
yearsEmployed is incremented.
Restrictions on variable names: Two strings have special meanings in SQLJ
programs. Observe the following restrictions when you use these strings in your
SQLJ programs:
Chapter 4. SQLJ application programming
111

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents