SQL Server: Cannot insert an explicit value into a

2020-01-27 06:19发布

When using this statement

create table demo (
    ts timestamp
)

insert into demo select current_timestamp

I get the following error:

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 do I insert the current time to a timestamp column?

7条回答
Rolldiameter
2楼-- · 2020-01-27 07:09

You can't insert the values into timestamp column explicitly. It is auto-generated. Do not use this column in your insert statement. Refer http://msdn.microsoft.com/en-us/library/ms182776(SQL.90).aspx for more details.

You could use a datetime instead of a timestamp like this:

create table demo (
    ts datetime
)

insert into demo select current_timestamp

select ts from demo

Returns:

2014-04-04 09:20:01.153
查看更多
登录 后发表回答