Obtains row format information about the result set of a prepared dynamic SQL statement.
For a list of possible SQL descriptor datatype codes, see Table 9-5.
exec sql describe [output] statement_name using sql descriptor descriptor_name end-exec
An optional keyword that has no effect on the describe output statement but provides conformance to the SQL standard.
The name (specified in a prepare statement) that represents the select statement to be executed.
Identifies descriptor_name as a SQL descriptor.
The name of a SQL descriptor that is to store the information returned by the describe output statement.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 QUERY PIC X(100).
01 NOUT PIC S9(9) COMP.
01 DATAVAL PIC X(100).
01 COUNTER PIC S9(9) COMP.
01 NUMCOLS PIC S9(9) COMP.
01 COLNAME PIC X(32).
01 COLTYPE PIC S9(9) COMP.
01 COLLEN PIC S9(9) COMP.
EXEC SQL END DECLARE SECTION END-EXEC.
...
DISPLAY "ENTER QUERY :"
ACCEPT QUERY.
EXEC SQL ALLOCATE DESCRIPTOR desc_out WITH MAX 256 END-EXEC.
EXEC SQL PREPARE dynstmt FROM :QUERY END-EXEC.
EXEC SQL DECLARE selcursor CURSOR FOR dynstmt END-EXEC.
EXEC SQL OPEN selcursor USING SQL DESCRIPTOR desc_out END-EXEC.
EXEC SQL DESCRIBE OUTPUT dynstmt USING SQL DESCRIPTOR desc_out END-EXEC.
PERFORM FETCH-LOOP UNTIL SQLCODE = 100.
EXEC SQL CLOSE selcursor END-EXEC.
EXEC SQL DEALLOCATE CURSOR selcursor END-EXEC.
EXEC SQL DEALLOCATE PREPARE dynstmt END-EXEC.
EXEC SQL DEALLOCATE DESCRIPTOR desc_out END-EXEC.
...
FETCH-LOOP.
EXEC SQL FETCH selcursor INTO SQL DESCRIPTOR desc_out END-EXEC
EXEC SQL GET DESCRIPTOR desc_out :NOUT = COUNT END-EXEC
DISPLAY "COLS RETRIEVED = ", NOUT
MOVE 1 TO COUNTER
PERFORM GET-DESC-PARA UNTIL COUNTER > NOUT.
END-FETCH-LOOP.
GET-DESC-PARA.
EXEC SQL GET DESCRIPTOR desc_out VALUE :COUNTER
:COLNAME = NAME,
:COLTYPE = TYPE,
:COLLEN = LENGTH
END-EXEC
DISPLAY "NAME :", COLNAME
DISPLAY "TYPE :", COLTYPE
DISPLAY "LENGTH :", COLLEN
EXEC SQL GET DESCRIPTOR desc_out VALUE :COUNTER
:DATAVAL = DATA END-EXEC
DISPLAY "DATA :", DATAVAL
DISPLAY " "
ADD 1 TO COUNTER.
END-GET-DESC-PARA.
The information obtained is the type, name, length (or precision and scale, if a number), nullable status, and number of items in the result set.
The information is about the result columns from the select column list.
Execute this statement before the prepared statement executes. If you perform a describe output statement after you execute and before you perform a get descriptor, the results will be discarded.
allocate descriptor, describe input, execute, get descriptor, prepare