简体   繁体   中英

Updating current time for a column in SQL

So i am trying to store the time at which any changes were made to any row. I was using the following query:

ALTER TABLE SPRD_MGMT_INP_INDEX_CHANGE_RATE
CHANGE "UPLOAD TIME"  
"UPLOAD TIME" TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;

I am getting the following error:

SQL compilation error: syntax error line 2 at position 0 unexpected 'CHANGE'. Please help

The syntax uses alter column and set , but the bigger issue is that you can't change the default of a column in Snowflake with the exception noted here:

https://docs.snowflake.com/en/sql-reference/sql/alter-table-column.html

In that page, it states the following:

Change the default for a column, unless the default is a sequence.

In that table for the action quoted above it has a checkmark by Unsupported .

You will need to create a new table with this default. You can then copy your rows from the original table using an insert from select.

You can specify the default in your new table like this:

create or replace table T1 
(COL1 int, COL2 string, UPLOAD_TIME timestamp_tz default current_timestamp()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM