Home:ALL Converter>Unable to add a new column to SQL Server table with TIMESTAMP data type

Unable to add a new column to SQL Server table with TIMESTAMP data type

Ask Time:2020-10-06T12:49:25         Author:Kevin

Json Formatter

I'm trying to add a new column to an existing SQL Server table with the data type of TIMESTAMP.

This is my script:

ALTER TABLE OrderDetails 
    ADD ModifiedTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

It should be not null. Running this script results in an error

Defaults cannot be created on columns of data type timestamp

I tried executing it without DEFAULT CURRENT_TIMESTAMP. But then it says

Cannot insert the value NULL into column 'ModifiedTime'

Any advises please?

Author:Kevin,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64219532/unable-to-add-a-new-column-to-sql-server-table-with-timestamp-data-type
marc_s :

TIMESTAMP in SQL Server has absolutely nothing to do with date & time (thanks to Sybase for screwing that one up!) - it's just a system-internal, binary counter (often used for opmistic concurrency checks). It's been deprecated, too, and renamed ROWVERSION which is much clearer as to what it is.\nFor date & time - use DATE (if you need only date - no time), or DATETIME2(n) datatypes:\nALTER TABLE OrderDetails \n ADD ModifiedTime DATETIME2(3) NOT NULL DEFAULT CURRENT_TIMESTAMP\n\nSee the official Microsoft docs for more details on date&time datatypes and functions",
2020-10-06T04:55:09
yy