SQLCODE -151: Column cannot be updated
SQLCODE -151 (SQLSTATE 42808) means an UPDATE tries to change a column that the Db2 catalog marks as not updatable. Typical cases are a view column that is calculated rather than taken from a table, and a catalog column that users may not change.
Other names for this code
Also written as SQL -151, SQLCODE=-151, SQLCODE = -151, db2 -151, sqlstate 42808, SQL0151N, column cannot be updated.
What it means
Db2 records, for every column of every table and view, whether it can be updated. The flag is the UPDATES column of SYSIBM.
A view column can only be updated if it maps straight to a column of a base table. If the view defines it as an expression, a constant, a function result or a column of another view that is itself not updatable, there is nowhere to put the new value. The rest of the view may still be updatable. Only that column is blocked.
The Db2 catalog is the other common place. Some catalog columns, such as many statistics columns, can be updated by authorized users. Others are maintained only by Db2, and an UPDATE on them gets -151.
This is different from -150, which means the whole view or table can't be changed.
Common causes
- A view column defined as an expression, such as
SALARY * 12 AS ANNUAL_, or as SUBSTR, CONCAT or another function.PAY - A view column defined as a constant, such as
'ACTIVE' AS STATUS. - A view built on another view whose column is already not updatable.
- An UPDATE on a column of a Db2 catalog table that Db2 doesn't let users change, often in a statistics or migration script.
How to find the problem
- Take the column name from the
DSNT408Imessage or SQLERRMC. - Check the flag with
SELECT NAME, UPDATES FROM SYSIBM.SYSCOLUMNS WHERE TBCREATOR = 'PAYPROD' AND TBNAME = 'EMP_. N means the column can't be updated.VIEW' - Read the view definition in SYSIBM.SYSVIEWS to see how the column is derived.
- For a catalog update, check the catalog table description in the SQL Reference for which columns can be updated.
How to fix it
- Update the base column the view column is derived from, through the view if that column is exposed, or on the base table.
- Leave calculated columns out of the SET clause. They change on their own when their source columns change.
- If users must update the column through the view, redefine the view so the column maps directly to a base column, or add an INSTEAD OF trigger.
- For catalog statistics, update only the columns IBM documents as updatable, or let RUNSTATS set them.
What it looks like
DSNT408I SQLCODE = -151, ERROR: THE UPDATE OPERATION IS INVALID BECAUSE THE CATALOG
DESCRIPTION OF COLUMN ANNUAL_PAY INDICATES THAT IT CANNOT BE UPDATED
DSNT418I SQLSTATE = 42808 SQLSTATE RETURN CODEIBM documentation
IBM documentation: -151. Use the documentation for your installed release.
Related codes
Questions
Why can I update some columns of the view but not others?
Each view column is checked on its own. Columns taken straight from a table can be updated, while calculated or constant columns can't.
What is the difference between -150 and -151?
-150 means the whole view or table doesn't allow the operation. -151 means the object allows updates, but the named column doesn't.
Is it the same on Db2 for LUW?
Yes. SQL0151N has SQLSTATE 42808 and also means the named column can't be updated.