Module 4: DB2 Data Types and SQL Basics
UPDATE
The UPDATE statement changes values in rows that already exist. The WHERE clause decides which rows change - and forgetting it changes every row in the table.
UPDATE syntax
- Basic shape: UPDATE table SET column = value WHERE condition.
- SET can change several columns at once, separated by commas.
- The new value can be a literal, a host variable, NULL, or an expression like SALARY * 1.10.
- Always check SQLCODE after UPDATE, then COMMIT to make the change permanent.
Why WHERE matters
- Without WHERE, UPDATE changes every row in the table. This is the most common beginner disaster.
- Safe habit: run the same condition as a SELECT first and count the rows.
- In SPUFI and interactive tools, some shops block UPDATE without WHERE completely.
- For wide updates, commit in batches so locks are not held too long.
Example
- Giving a 10 percent raise to one department:-
EXEC SQL
UPDATE EMP
SET SALARY = SALARY * 1.10
WHERE DEPTNO = 'D11'
END-EXEC.
IF SQLCODE = 0
DISPLAY 'ROWS UPDATED: ' SQLERRD(3)
END-IF.
