开发者

SQL Server 2008 datetime column JDBC insert problem

开发者 https://www.devze.com 2023-02-02 04:57 出处:网络
I have a table with a column that is a DATETIME in a SQL Server 2008 database.The dates that are being inserted are not timestamps in the sense that using NOW on insert would be acceptable.The date is

I have a table with a column that is a DATETIME in a SQL Server 2008 database. The dates that are being inserted are not timestamps in the sense that using NOW on insert would be acceptable. The date is generated a bit before the insert and I would like to preserve this value.

Datetimes are being serialized as strings in the format yyyy-mm-dd hh:mm:ss.[fff...] and then converted to a JDBC Timestamp object before being inserted.

A problem I'm running into with SQL Server is that it converts the v开发者_StackOverflowalue to TIMESTAMP before being inserted and then complains with this:

com.microsoft.sqlserver.jdbc.SQLServerException: 
Cannot insert an explicit value into a timestamp column. 
Use INSERT with a column list to exclude the timestamp column, 
or insert a DEFAULT into the timestamp column.

How can I just insert into a DATETIME column without the conversion?

Thanks.


A SQL Server TIMESTAMP data type is not a date-like data type. It is a server-generated value used to help with data consistency. If you are storing datetime data, then avoid the TIMESTAMP data type.

So, don't convert to a JDBC Timestamp object. Data can be inserted as a string value. Note that the format you indicate is okay, but the fff... must be 3 digits, not more than 3 digits for a SQL Server DATETIME data type.


If the column is defined as a TimeStamp here's a quote from SQL Server Books Online:

The SQL Server timestamp data type has nothing to do with times or dates. SQL Server timestamps are binary numbers that indicate the relative sequence in which data modifications took place in a database. The timestamp data type was originally implemented to support the SQL Server recovery algorithms.

It further states Never use timestamp columns in keys, especially primary keys, because the timestamp value changes every time the row is modified.

0

精彩评论

暂无评论...
验证码 换一张
取 消