SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)
Dynamic SQL
HP NonStop SQL/MX Programming Manual for C and COBOL—523627-004
9-4
Declare a Host Variable for the Dynamic SQL
Statement
Figure 9-2 shows the steps presented within the complete COBOL program. These
steps are executed in the sample program Example C-3 on page C-6.
For more information:
1. Declare a Host Variable for the Dynamic SQL Statement on page 9-4
2. Move the Statement Into the Host Variable on page 9-5
3. Prepare the SQL Statement on page 9-5
4. Set Explicit Input Values on page 9-6
5. Execute the Prepared Statement on page 9-6
6. Deallocate the Prepared Statement on page 9-7
Declare a Host Variable for the Dynamic SQL Statement
In an SQL Declare Section, declare a host variable to use as a container for the
dynamic SQL statement. You specify this host variable when you prepare the SQL
statement. You must declare the host variable:
•
Before the PREPARE statement
•
Within the same scope as the PREPARE statement
Figure 9-2. Using Dynamic SQL in a COBOL Program
...
EXEC SQL BEGIN DECLARE SECTION;
01 HV-SQL-STATEMENT PIC X(256).
01 IN-VALUE PIC 9(5) COMP.
01 HV-COLUMN1 ...
01 HV-COLUMN2 ...
...
EXEC SQL END DECLARE SECTION;
* Construct the SQL statement and move to statement variable.
...
EXEC SQL PREPARE sqlstmt FROM :HV-SQL-STATEMENT END-EXEC.
...
* Prompt user for value of input parameter.
...
ACCEPT IN-VALUE.
EXEC SQL EXECUTE sqlstmt USING :IN-VALUE
INTO :HV-COLUMN1,:HV-COLUMN2,... END-EXEC.
EXEC SQL DEALLOCATE PREPARE sqlstmt END-EXEC.
...
COBOL
1
2
3
4
5
6