SQL/MX Programming Manual for Java

SQL/MX Programming Considerations
HP NonStop SQL/MX Programming Manual for Java523726-003
4-18
SET Statements for Dynamic SQL
#sql [ctx2] { /* Static SQL statement 2 */ };
ctx2.close();
SET Statements for Dynamic SQL
The SET statements are executable statements that affect only dynamic SQL
statements with the same JDBC connection in an SQLJ program.
Placement of the SET Statements
Place the SET statements anywhere executable statements are allowed in an SQLJ
program. You cannot place these statements at the top-level scope of a program where
you code class declarations. If you associate a connection context with a SET
statement, place the SET statement after the instantiation of the connection context
object.
Control Flow Scope of the SET Statements
SET statements have control flow scope in an SQLJ program. That is, a SET
statement affects only those dynamic statements that share the same JDBC
connection and that execute after the SET statement executes in the program.
Example
Suppose that the SQLJ program was customized with -missingSQLObject set to
true, and a few of the SQL statements referred to unavailable database objects at the
time of customization. Therefore, some SQL statements are dynamic, while others are
static in this program.
In this example, the SET CATALOG statements do not affect the static SQL
statements. However, the first SET CATALOG statement sets the catalog of the
dynamic statements to CAT1 if the department number (deptnum) is 3100 at run time.
The second SET CATALOG statement sets the catalog of the dynamic statements to
CAT2 if
deptnum is not 3100 at run time.
try {
if (deptnum == 3100)
{
#sql [ctx] {SET CATALOG cat1};
}
else
{
#sql [ctx] {SET CATALOG cat2};
}
#sql [ctx] { /* Static SELECT statement */ };
#sql [ctx] { /* Dynamic UPDATE statement */ };
#sql [ctx] { /* Static UPDATE statement */ };
#sql [ctx] { /* Dynamic SELECT statement */ };
}