SQL$HELP72.HLB  —  DESCRIBE  Examples
    Example 1: Using the DESCRIBE . . . OUTPUT statement with a
    prepared SELECT statement

    This PL/I program illustrates using the DESCRIBE . . . OUTPUT
    statement to write information to the SQLDA about the select
    list items of a prepared SELECT statement. There are no parameter
    markers in this particular prepared SELECT statement.

    After issuing the DESCRIBE statement, the program stores in the
    SQLDA the addresses of host language variables that will receive
    values from columns of the result table during FETCH statements.

    To shorten the example, this PL/I program is simplified:

    o  The program includes the SELECT statement to be dynamically
       executed as part of the source code directly in the PREPARE
       statement. A program with such coded SQL statements does
       not need to use dynamic SQL at all, but can simply embed the
       SELECT statement in a DECLARE CURSOR statement. (A program
       that must process SQL statements generated as it executes is
       the only type that requires dynamic SQL.)

    o  The program declares host language variables for select list
       items without checking the SQLDA for a description of those
       items. Typically, an application needs to look in the SQLDA
       to determine the number and data type of select list items
       generated by a prepared SELECT statement before allocating
       storage.

    o  The program does not use the DESCRIBE . . . INPUT statement
       to determine if there are any parameter markers in this
       dynamically executed SELECT statement. In this example,
       because the SELECT statement is coded in the program, it
       is clear that there is no need for a DESCRIBE . . . INPUT
       statement. However, if the SELECT statement is generated
       at run time, the program may have to determine there if are
       parameter markers by issuing a DESCRIBE . . . INPUT statement
       and looking at the value of the SQLD field in the SQLDA.

    CURSOR_EX : PROCEDURE OPTIONS (MAIN);
    /*
    * Illustrate the DESCRIBE...SELECT LIST statement using a
    * dynamic SELECT statement:
    *
    * Use a dynamic SELECT statement as the basis for
    * a cursor that displays a join of the EMPLOYEES
    * and SALARY_HISTORY tables on the screen.
    */
    declare sys$putmsg external entry
    (any, any value, any value, any value);

    /* Declare SQL Communications Area: */
    EXEC SQL INCLUDE SQLCA;

    /* Declare SQL Descriptor Area: */
    EXEC SQL INCLUDE SQLDA;

    /* Declare the alias: */
    EXEC SQL DECLARE ALIAS FILENAME 'SQL$DATABASE';

    /*
     * Branch to ERR_HANDLER if the SQLCODE field
     * of the SQLCA is greater than 0:
     */
    EXEC SQL WHENEVER SQLERROR GOTO ERR_HANDLER;

    /*
     * Declare a cursor named EMP that uses the
     * prepared statement DYN_SELECT:
     */
    EXEC SQL DECLARE EMP CURSOR FOR DYN_SELECT;

    /* Declare a host structure to receive
     * the results of FETCH statements:
     */
    DCL 1 P_REC,
            2 EMPLOYEE_ID   CHAR(5),
            2 FIRST_NAME    CHAR(10),
            2 LAST_NAME     CHAR(14),
            2 SALARY_AMOUNT FIXED BINARY(31);

    /* Allocate memory for the SQLDA and
     * set the value of its SQLN field:
     */
    SQLSIZE = 10;
    ALLOCATE SQLDA SET (SQLDAPTR);
    SQLN = 10;

    /* Prepare the SELECT statement
     * for dynamic execution directly
     * from a statement string:
     */
    EXEC SQL PREPARE DYN_SELECT FROM
            'SELECT E.EMPLOYEE_ID,
                    E.FIRST_NAME,
                    E.LAST_NAME,
                    S.SALARY_AMOUNT
            FROM  EMPLOYEES E, SALARY_HISTORY S
            WHERE E.EMPLOYEE_ID = S.EMPLOYEE_ID AND
                  S.SALARY_END IS NULL';

    /* Write information about the
     * columns of the result table
     * of DYN_SELECT into the SQLDA:
     */
    EXEC SQL DESCRIBE DYN_SELECT OUTPUT INTO SQLDA;

    /*
     * Assign the addresses of the host language
     * variables that will receive the values of the
     * fetched row to the SQLDATA field
     * of the SQLDA:
     */
    SQLDATA(1) = ADDR( EMPLOYEE_ID );
    SQLDATA(2) = ADDR( FIRST_NAME);
    SQLDATA(3) = ADDR( LAST_NAME );
    SQLDATA(4) = ADDR( SALARY_AMOUNT);

    /* Open the cursor: */
    EXEC SQL OPEN EMP;

    /* Fetch the first row of the result table.
     * SQL uses the addresses in the SQLDA
     * to store values from the table into
     * the host language variables.
     */
    EXEC SQL FETCH EMP USING DESCRIPTOR SQLDA;

    PUT EDIT ('Current Salaries of Employees: ') (SKIP, A, SKIP(2));

    /* While the SQLCODE field of the
     * SQLCA is not 100 (NOT_FOUND error):
     */
    DO WHILE (SQLCA.SQLCODE = 0);

            /* Display the values from the host language variables: */
            PUT SKIP EDIT
            (EMPLOYEE_ID, ' ', FIRST_NAME, ' ', LAST_NAME, ' ',
             SALARY_AMOUNT)
            (A, A, A, A, A, A, F(9));

            /* Fetch another row of the result table: */
            EXEC SQL FETCH EMP USING DESCRIPTOR SQLDA;
    END;

    /* Close the cursor: */
    EXEC SQL CLOSE EMP;

    RETURN;

    ERR_HANDLER:
            PUT EDIT
            ('Unexpected error, SQLCODE is: ', SQLCA.SQLCODE) (skip, a, f(9));
            CALL SYS$PUTMSG(RDB$MESSAGE_VECTOR, 0, 0, 0);
            EXEC SQL WHENEVER SQLERROR CONTINUE;
            EXEC SQL ROLLBACK;
            RETURN;
    END CURSOR_EX;

    See also Example 2 in DECLARE Dynamic_CURSOR.
Close Help