3. Embedded SQL for COBOL : COBOL Data Items and Data Types : Variable Usage
 
Share this page                  
Variable Usage
COBOL variables (that is, data items) declared in an embedded SQL declaration section can substitute for most elements of embedded SQL statements that are not keywords. Of course, the variable and its data type must make sense in the context of the element. When you use a COBOL variable in an embedded SQL statement, you must precede it with a colon. As an example, the following select statement uses the data items NAMEVAR and NUMVAR to receive data and the data item IDNO as an expression in the where clause.
Example: Variable declarations
EXEC SQL SELECT ename, eno
    INTO :NAMEVAR, :NUMVAR
    FROM employee
    WHERE eno = :IDNO END-EXEC.
Various rules and restrictions apply to the use of COBOL variables in embedded SQL statements. The following sections describe the usage syntax of different categories of variables and provide examples of such use.
To distinguish the minus sign used as a subtraction operator in an embedded SQL statement from the hyphen used as a character in a data item name, you must delimit the minus sign by blanks. For example, the statement:
EXEC SQL INSERT INTO employee (ename, eno)
    VALUES ('Jones', :ENO-2)
    END EXEC.
indicates that the data item ENO-2 is to be inserted into the database column. To insert a value two less than the value in the data item ENO, you must instead use the following statement:
EXEC SQL INSERT INTO employee (ename, eno)
    VALUES ('Jones', :ENO - 2)
    END EXEC.
Note the spaces surrounding the minus sign.