SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)
Dynamic SQL With Descriptor Areas
HP NonStop SQL/MX Programming Manual for C and COBOL—523627-004
10-13
Steps for Using SQL Item Descriptor Areas
Figure 10-2 shows the steps presented within the complete COBOL program. These 
steps are executed in the sample program Example C-4 on page C-9.
For more information:
1. Declare a Host Variable for the Dynamic SQL Statement on page 10-14
2. Construct the SQL Statement From User Input on page 10-14
3. Allocate Input and Output SQL Descriptor Areas on page 10-14
Figure 10-2. Using SQL Descriptor Areas in a COBOL Program
 EXEC SQL BEGIN DECLARE SECTION; 
 01 hv-sql-statement PIC X(256).
 01 in-value PIC 9(5) COMP.
 01 num PIC 9(4) COMP. 
* Declare host variables for item descriptor values. 
 01 num-data PIC S9(9) COMP. 
 01 char-data PIC X(100). 
 ...
 EXEC SQL END DECLARE SECTION;
* Construct the SQL statement from user input. 
 ...
 MOVE 1 TO desc-max.
 EXEC SQL ALLOCATE DESCRIPTOR 'in_sqlda' WITH MAX :desc-max ...
 MOVE 6 TO desc-max.
 EXEC SQL ALLOCATE DESCRIPTOR 'out_sqlda' WITH MAX :desc-max ...
 EXEC SQL PREPARE sqlstmt FROM :hv-sql-statement END-EXEC.
 ...
 EXEC SQL DESCRIBE INPUT sqlstmt 
 USING SQL DESCRIPTOR 'in_sqlda' END-EXEC. 
 EXEC SQL DESCRIBE OUTPUT sqlstmt 
 USING SQL DESCRIPTOR 'out_sqlda' END-EXEC.
 MOVE 1 TO desc-value.
 EXEC SQL SET DESCRIPTOR 'in_sqlda' VALUE :desc-value 
 VARIABLE_DATA = :in-value END-EXEC.
 EXEC SQL EXECUTE sqlstmt USING SQL DESCRIPTOR 'in_sqlda'
 INTO SQL DESCRIPTOR 'out_sqlda' END-EXEC.
 EXEC SQL GET DESCRIPTOR 'out_sqlda' :num = COUNT END-EXEC.
 PERFORM VARYING i FROM 1 BY 1 UNTIL i > num 
 EXEC SQL GET DESCRIPTOR 'out_sqlda' VALUE :i
 :type = TYPE, 
 :length = LENGTH, 
 :name = NAME 
 END-EXEC. 
* Test type or name to determine the compatible host variable. 
 if ...
 EXEC SQL GET DESCRIPTOR 'out_sqlda' VALUE :i 
 :num-data = VARIABLE_DATA 
 END-EXEC.
 END-PERFORM.
* Process the values from the item descriptor areas. 
 EXEC SQL DEALLOCATE PREPARE sqlstmt END-EXEC.
 EXEC SQL DEALLOCATE DESCRIPTOR 'in_sqlda' END-EXEC.
 EXEC SQL DEALLOCATE DESCRIPTOR 'out_sqlda' END-EXEC.
COBOL
1
2
3
4
5
6
7
8
9










