Question

In my table I've a column MODIFIED with type TIMESTAMP. I want to update this column automatically when the row is updated

I can use this for a new column

alter table my_table add column last_updated timestamp not null
 generated by default for each row on update as row change timestamp 

But I can't use this to edit the existing column to do the updating automatically

alter table my_table alter column modified set data type timestamp 
 not null generated by default for each row on update as row change timestamp

Am I using the alter correctly?

the error message is

Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=alter table my_table alter column modified;BEGIN-OF-STATEMENT;<values>, DRIVER=3.50.152
SQLState:  42601
ErrorCode: -104
Was it helpful?

Solution

Found an article here http://ibmsystemsmag.blogs.com/db2utor/2008/01/automate-row-ch.html

essentially it says

  • You cannot use the ALTER TABLE statement to convert a column on an existing table to use the as-row-change-timestamp-clause. You must either drop the table and recreate with the column defined with the as-row-change-timestamp-clause or ALTER the table and add a new column with the as-row-change-timestamp-clause.

OTHER TIPS

You will be able to add new column with timestamp on using below query.

ALTER TABLE TABLE_NAME
  add column UPDATED_DATE_TIME TIMESTAMP GENERATED ALWAYS FOR EACH ROW
  ON UPDATE AS ROW CHANGE TIMESTAMP NOT NULL;

Once the column exists, you use a slightly different syntax to alter the data type and attributes:

alter table my_table alter column my_column set generated by default ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top