Updating data by using host variables

When you want to update a value in a Db2 table, but you do not know the exact value until the program runs, use host variables. Db2 can change a table value to match the current value of the host variable.

Procedure

To update data by using host variables:

  1. Declare the necessary host variables.
  2. Specify an UPDATE statement with the appropriate host variable names in the SET clause.

Examples

Example of updating a single row by using a host variable
The following COBOL example changes an employee's phone number to the value in the NEWPHONE host variable. The employee ID value is passed through the EMPID host variable.
MOVE '4246' TO NEWPHONE.
MOVE '000110' TO EMPID.
EXEC SQL
  UPDATE DSN8C10.EMP
    SET PHONENO = :NEWPHONE
    WHERE EMPNO = :EMPID
END-EXEC.
Example of updating a single row by using a host variable
The following example gives the employees in a particular department a salary increase of 10%. The department value is passed through the DEPTID host variable.
MOVE 'D11' TO DEPTID.
EXEC SQL
  UPDATE DSN8C10.EMP
    SET SALARY = 1.10 * SALARY
    WHERE WORKDEPT = :DEPTID
END-EXEC.