Documents a statement name later used in a PREPARE statement in dynamic SQL. SQL does not require DECLARE STATEMENT statements and does not generate any code when it precompiles them. They are entirely optional.
1 – Environment
You can issue the DECLARE STATEMENT statement only in host language programs to be precompiled.
2 – Format
(B)0[m[1;4mDECLARE[m[1m qqwq> <statement-name> wq> [1;4mSTATEMENT[m[1m [m [1m mqqqqqqq , <qqqqqqqqj [m
3 – Arguments
3.1 – statement-name
Specifies the name of a statement later referred to in one of the following embedded dynamic statements: o PREPARE o DECLARE CURSOR o DESCRIBE
4 – Example
Example 1: Declaring a statement name in a PL/I program This example shows a program line that declares a statement name DYNAMIC_STATEMENT. Later lines in the example show how DECLARE CURSOR, PREPARE, and DESCRIBE statements refer to it. Because you do not have to declare a statement explicitly, the DECLARE STATEMENT statement is always optional. EXEC SQL DECLARE DYNAMIC_STATEMENT STATEMENT; /* Declare the SQL Communications Area. */ EXEC SQL INCLUDE SQLCA; /* Declare the SQL Descriptor Area. */ EXEC SQL INCLUDE SQLDA; /* The program declares the host language variable STATEMENT_STRING and stores in it the character string containing a SELECT statement to be executed dynamically. */ . . . EXEC SQL DECLARE CURSOR1 CURSOR FOR DYNAMIC_STATEMENT; EXEC SQL PREPARE OBJECT_STATEMENT FROM STATEMENT_STRING; EXEC SQL DESCRIBE OBJECT_STATEMENT INTO SQLDA; /* The program sets up pointers in the SQLDATA field of the SQLDA to the data area (host language variables or dynamic memory, for example) to receive the data from the cursor. */ . . . EXEC SQL OPEN CURSOR1; DO WHILE (SQLCODE = 0); EXEC SQL FETCH CURSOR1 USING DESCRIPTOR SQLDA; /* The program prints or otherwise processes rows of the result tables. */ . . . END; EXEC SQL CLOSE CURSOR1;