Change data in a table using the UPDATE statement

This topic shows the basic SQL statement and clauses that update data into tables and views. To change the data in a table, use the UPDATE statement.

With the UPDATE statement, you can change the value of one or more columns in each row that meets the search condition of the WHERE clause. The result of the UPDATE statement is one or more changed column values in zero or more rows of a table (depending on how many rows meet the search condition specified in the WHERE clause). The UPDATE statement looks like this:

   UPDATE table-name
     SET column-1 = value-1,
         column-2 = value-2, ...
     WHERE search-condition ...

For example, suppose an employee was relocated. To update several items of the employee's data in the CORPDATA.EMPLOYEE table to reflect the move, you can specify:

   UPDATE CORPDATA.EMPLOYEE
     SET JOB = :PGM-CODE,
         PHONENO = :PGM-PHONE
     WHERE EMPNO = :PGM-SERIAL

Use the SET clause to specify a new value for each column you want to update. The SET clause names the columns you want updated and provides the values you want them changed to. The value you specify can be:

The following is an example of a statement that uses many different values:

   UPDATE WORKTABLE
      SET COL1 = 'ASC',
          COL2 = NULL,
          COL3 = :FIELD3,
          COL4 = CURRENT TIME,
          COL5 = AMT - 6.00,
          COL6 = COL7
       WHERE EMPNO = :PGM-SERIAL

To identify the rows to be updated, use the WHERE clause:

You can omit the WHERE clause. If you do, SQL updates each row in the table or view with the values you supply.

If the database manager finds an error while running your UPDATE statement, it stops updating and returns a negative SQLCODE. If you specify COMMIT(*ALL), COMMIT(*CS), COMMIT(*CHG), or COMMIT(*RR), no rows in the table are changed (rows already changed by this statement, if any, are restored to their previous values). If COMMIT(*NONE) is specified, any rows already changed are not restored to previous values.

If the database manager cannot find any rows that meet the search condition, an SQLCODE of +100 is returned.

Note: The UPDATE statement may have updated more than one row. The number of rows updated is reflected in SQLERRD(3) of the SQLCA. This value is also available from the ROW_COUNT diagnostics item in the GET DIAGNOSTICS statement.

The SET clause of an UPDATE statement can be used in many ways to determine the actual values to be set in each row being updated. The following example lists each column with its corresponding value:

UPDATE EMPLOYEE
  SET WORKDEPT = 'D11',
      PHONENO = '7213',
      JOB = 'DESIGNER'
  WHERE EMPNO = '000270'

The previous update can also be written by specifying all of the columns and then all of the values:

UPDATE EMPLOYEE
  SET (WORKDEPT, PHONENO, JOB)
      = ('D11', '7213', 'DESIGNER')
  WHERE EMPNO = '000270'
Related information
UPDATE statement