3. Embedded SQL for COBOL : Dynamic Programming for COBOL : Pointer Usage with COBOL Data Items
 
Share this page                  
Pointer Usage with COBOL Data Items
In order to fill an element of the sqlvar array, you must set the type information and assign a valid address to sqldata. The address must be that of a legally declared data item. If the element is nullable, the sqlind field must point at a legally declared null indicator.
As a concluding example, the following fragment sets the type information of, and points at, a 4‑byte integer data item, an 8‑byte nullable floating-point data item, and an sqllen‑specified character sub‑string. The following examples demonstrate how a program can maintain a pool of available data items, such as large arrays of the few different typed variables and a large string space. The next available spot is chosen from the pool.
Windows and UNIX
* Assume SQLDA has been declared, as well as the
* following COBOL tables:
*   INT-4-TABLE, FLOAT-TABLE and INDICATOR-TABLE
* Also assume that a large character string buffer has
* been declared:
* CHAR-STRING
  MOVE 30        TO SQLTYPE(1).
  MOVE 4         TO SQLLEN(1).
  SET SQLIND(1)  TO NULL.
  SET SQLDATA(1) TO ADDRESS OF INT-4-TABLE(CUR-INT).
  ADD 1          TO CUR-INT.
  MOVE -31       TO SQLTYPE(2).
  MOVE 8         TO SQLLEN(2).
  SET SQLIND(2)  TO ADDRESS OF INDICATOR-TABLE(CUR-IND).
  SET SQLDATA(2) TO ADDRESS OF FLOAT-8-TABLE(CUR-FLT).
  ADD 1          TO CUR-IND.
  ADD 1          TO CUR-FLT.

* SQLLEN has been assigned by DESCRIBE to be the length
* of a specific result column. This length is used to
* pick off a sub-string out of a large character string
* space.

  MOVE SQLLEN(3)   TO NEED-LEN.
  MOVE 20          TO SQLTYPE(3).
  SET SQLIND(3)    TO NULL.
  SET SQLDATA(3)   TO ADDRESS OF
         CHAR-STRING(CUR-CHAR:NEED-LEN).
ADD NEED-LEN     TO CUR-CHAR.
It is advisable to set sqlind to point to a null address if the data represented by the sqlvar element is not nullable, that is, the sqlvar.sqltype is positive. However, because some COBOL compilers do not accept the SET pointer‑item TO NULL syntax, Ingres will ignore the sqlind pointer if the sqltype is positive, which allows you to leave out that particular step if necessary.
VMS
* Assume SQLDA has been declared, as well as the
* following COBOL tables:
*     INT-4-TABLE, FLOAT-8-TABLE and INDICATOR-TABLE
* Also assume that a large character string buffer has
* been declared:
*     CHAR-STRING
    MOVE 30         TO SQLTYPE(1).
    MOVE 4          TO SQLLEN(1).
    MOVE 0          TO SQLIND(1).
    SET SQLDATA(1)  TO REFERENCE
                     OF INT-4-TABLE(CUR-INT).
    ADD 1           TO CUR-INT.

    MOVE -31        TO SQLTYPE(2).
    MOVE 8          TO SQLLEN(2).
    SET SQLIND(2)   TO REFERENCE
                    OF INDICATOR-TABLE(CUR-IND).
    SET SQLDATA(2)  TO REFERENCE
                     OF FLOAT-8-TABLE(CUR-FLT).
    ADD 1           TO CUR-IND.
    ADD 1           TO CUR-FLT.

* SQLLEN has been assigned by DESCRIBE to be the length
* of a specific result column. This length is used to
* pick off a sub-string out of a large character
* string space.

    MOVE SQLLEN(3)  TO NEED-LEN.
    MOVE 20         TO SQLTYPE(3).
    MOVE 0          TO SQLIND(3).
    SET SQLDATA(3)  TO REFERENCE OF
    CHAR-STRING(CUR-CHAR:NEED-LEN).
    ADD NEED-LEN    TO CUR-CHAR.