Sql Server: Cannot insert an explicit value into a timestamp column
The TIMESTAMP
data type in SQL Server is a system data type, not designed for storing date or time values. If you're looking for row versioning, ROWVERSION
—equivalent to TIMESTAMP
—is the right choice. For actual time data, use DATETIME
or DATETIME2
with a DEFAULT
constraint for time stamping:
Need any ROWVERSION
? No problem:
Remember, let ROWVERSION
column auto-magically update on any insert/update, no user intervention required.
Timestamp: The Cinderella's shoe of SQL Server
The TIMESTAMP
data type is SQL Server's Cinderella's shoe—it's unique for every row. It's just not meant for date or time values. For that, you've DATETIME2
with greater precision and range.
Dealing with datetime
Use DATETIME2
for storing your application timestamps. But be cautious with converting from TIMESTAMP
to preserve data integrity.
High-level data scenarios
If your data integrity needs are exceptionally high or you are dealing with system-generated data, consider binary(8)
or varbinary(8)
. However, this sacrifices simplicity, so evaluate properly.
Advice for the seasoned coder
When copying or moving data, always exempt the TIMESTAMP
or ROWVERSION
from your SELECT
list. In scenarios involving data movement, ensure all system-generated columns are treated as metadata and not as insertable data points.
A word of warning
If legacy constraints force you to transform a TIMESTAMP
to a DATETIME
, know the resulting value is as meaningful as a snowmobile in the Sahara. Be cautious and keep such conversions to a minimum.
Ready for the switch?
If your table is plagued with an epidemic of TIMESTAMP
and you're considering a switch to more accurate data types, you're in for a bit of schema migration. Handle changes with careful data management practices to prevent data loss or corruption.
Visualization
The timestamp column is like an internal clock within SQL Server:
But you can't manually set a timestamp column:
So remember:
Think of timestamps in SQL Server like a self-driving car:
Was this article helpful?