You can code SQL statements in your PL/I applications using the language defined in SQL Reference, Volume 1 and Volume 2 (SBOF-8923). Specific requirements for your SQL code are described in the sections that follow.
A PL/I program that contains SQL statements must include an SQL communications area (SQLCA) As shown in Figure 1 part of an SQLCA consists of an SQLCODE variable and an SQLSTATE variable.
The SQLCA should be included by using the SQL INCLUDE statement:
exec sql include sqlca;
The SQLCA must not be defined within an SQL declare section. The scope of the SQLCODE and SQLSTATE declaration must include the scope of all SQL statements in the program.
Dcl
1 Sqlca,
2 sqlcaid char(8), /* Eyecatcher = 'SQLCA '*/
2 sqlcabc fixed binary(31), /* SQLCA size in bytes = 136 */
2 sqlcode fixed binary(31), /* SQL return code */
2 sqlerrm char(70) var, /* Error message tokens */
2 sqlerrp char(8), /* Diagnostic information */
2 sqlerrd(6) fixed binary(31), /* Diagnostic information */
2 sqlwarn, /* Warning flags */
3 sqlwarn0 char(1),
3 sqlwarn1 char(1),
3 sqlwarn2 char(1),
3 sqlwarn3 char(1),
3 sqlwarn4 char(1),
3 sqlwarn5 char(1),
3 sqlwarn6 char(1),
3 sqlwarn7 char(1),
2 sqlext,
3 sqlwarn8 char(1),
3 sqlwarn9 char(1),
3 sqlwarna char(1),
3 sqlstate char(5); /* State corresponding to SQLCODE */The following statements require an SQLDA:
Unlike the SQLCA, there can be more than one SQLDA in a program, and an SQLDA can have any valid name. An SQLDA should be included by using the SQL INCLUDE statement:
exec sql include sqlda;
The SQLDA must not be defined within an SQL declare section.
Dcl
1 Sqlda based(Sqldaptr),
2 sqldaid char(8), /* Eye catcher = 'SQLDA ' */
2 sqldabc fixed binary(31), /* SQLDA size in bytes=16+44*SQLN*/
2 sqln fixed binary(15), /* Number of SQLVAR elements*/
2 sqld fixed binary(15), /* # of used SQLVAR elements*/
2 sqlvar(Sqlsize refer(sqln)), /* Variable Description */
3 sqltype fixed binary(15), /* Variable data type */
3 sqllen fixed binary(15), /* Variable data length */
3 sqldata pointer, /* Pointer to variable data value*/
3 sqlind pointer, /* Pointer to Null indicator*/
3 sqlname char(30) var ; /* Variable Name */
dcl Sqlsize fixed binary(15); /* number of sqlvars (sqln) */
dcl Sqldaptr pointer;The first statement of your PL/I program must be a PROCEDURE or a PACKAGE statement. You can add SQL statements to your program wherever executable statements can appear. Each SQL statement must begin with EXEC (or EXECUTE) SQL and end with a semicolon (;).
For example, an UPDATE statement might be coded as follows:
exec sql update Department
export Mgrno = :Mgr_Num
where Deptno = :Int_Dept;
In addition to SQL statements, PL/I comments can be included in embedded SQL statements wherever a blank is allowed.
The line continuation rules for SQL statements are the same as those for other PL/I statements.
SQL statements or PL/I host variable declaration statements can be included by placing the following SQL statement at the point in the source code where the statements are to be embedded:
exec sql include member;
SQL statements must be coded in columns m through n where m and n are specified in the MARGINS(m,n) compile-time option.
Any valid PL/I variable name can be used for a host variable and is subject to the following restriction: Do not use host variable names, external entry names, or access plan names that begin with 'SQL', 'DSN', or 'IBM'. These names are reserved for the database manager or PL/I. The length of a host variable name must not exceed 100 characters.
With the exception of the END DECLARE SECTION statement, and the INCLUDE text-file-name statement, executable SQL statements, like PL/I statements, can have a label prefix.
The target for the GOTO clause in an SQL WHENEVER statement must be a label in the PL/I source code and must be within the scope of any SQL statements affected by the WHENEVER statement.
All host variables used in SQL statements must be explicitly declared. If ONEPASS is in effect, a host variable used in an SQL statement must be declared prior to the first use of the host variable in an SQL statement. In addition:
Host variable declarations can be made at the same place as regular PL/I variable declarations.
Only a subset of valid PL/I declarations are recognized as valid host variable declarations. The preprocessor does not use the data attribute defaults specified in the PL/I DEFAULT statement. If the declaration for a variable is not recognized, any statement that references the variable might result in the message "The host variable token ID is not valid".
Only the names and data attributes of the variables are used by the preprocessor; the alignment, scope, and storage attributes are ignored.
The following figure shows the syntax for valid numeric host variable declarations.
>>-+-DECLARE-+--+-variable-name---------+-----------------------> '-DCL-----' | .-,-------------. | | V | | '-(---variable-name-+-)-' >----+---------+-+-FIXED--+-------------------------+-+---------> +-BINARY--+ | '-(precision-+--------+-)-' | +-BIN-----+ | '-,scale-' | +-DECIMAL-+ '-FLOAT-+-----------------+----------' '-DEC-----' '-(--precision--)-' >--+---------------------------------------+-- ; -------------->< '-Alignment and/or Scope and/or Storage-'
Notes
The following figure shows the syntax for valid character host variables.
>>-+-DECLARE-+--+-variable-name---------+-----------------------> '-DCL-----' | .-,-------------. | | V | | '-(---variable-name-+-)-' >--+-CHARACTER-+--+----------+--+---------+---------------------> '-CHAR------' '-(length)-' +-VARYING-+ '-VAR-----' >--+---------------------------------------+-- ; -------------->< '-Alignment and/or Scope and/or Storage-'
Notes
The following figure shows the syntax for valid graphic host variables.
>>-+-DECLARE-+--+-variable-name---------+-----------------------> '-DCL-----' | .-,-------------. | | V | | '-(---variable-name-+-)-' >--GRAPHIC--+----------+--+---------+---------------------------> '-(length)-' +-VARYING-+ '-VAR-----' >--+---------------------------------------+-- ; -------------->< '-Alignment and/or Scope and/or Storage-'
Notes