SQL服务器:无法插入显式值时间戳列SQL服务器:无法插入显式值时间戳列(SQL Server: C

2019-05-13 15:00发布

当使用此声明

create table demo (
    ts timestamp
)

insert into demo select current_timestamp

我得到以下错误:

无法插入显式值时间戳列。 使用INSERT列的列表来排除时间戳列,或插入到DEFAULT时间戳列

我要如何插入当前时间的timestamp列?

Answer 1:

根据MSDN , timestamp

那是一个数据库中公开自动生成的,唯一的二进制数字的数据类型。 时间戳通常用作版本冲压表中的行的机构。 存储大小为8个字节。 timestamp数据类型只是一个递增的数字,不保留日期或时间。 要记录日期或时间,使用日期时间数据类型。

你可能寻找的datetime数据类型来代替。



Answer 2:

如果你有一个需要复制完全相同的时间戳数据,目标表中的数据类型由时间戳更改为二进制(8) - 我用VARBINARY(8),它工作得很好。

这显然打破了目标表中的任何时间戳功能,所以一定要确保你行与第一位。



Answer 3:

你不能值插入时间戳列明确。 这是自动生成的。 请不要在INSERT语句中使用此列。 请参阅http://msdn.microsoft.com/en-us/library/ms182776(SQL.90).aspx了解更多详情。

你可以使用日期时间,而不是像这样的时间戳:

create table demo (
    ts datetime
)

insert into demo select current_timestamp

select ts from demo

返回:

2014-04-04 09:20:01.153


Answer 4:

如何插入当前时间与SQL Server中的时间戳:

答:不能,这里的原因。

在SQL Server的新版本, timestamp被重命名为RowVersion 。 这是正确的,因为时间戳是非常误导的。

SQL Server时间戳不是由用户设定,并不代表日期或时间。 时间戳是连续编号的只是二进制表示,这只是为确保行并没有改变,因为它已经读过好。

如果你想存储日期或时间,不使用时间戳,则必须使用其他数据类型之一,例如像datetimesmalldatetimedatetimeDATETIME2

例如:

create table wtf (
    id INT,
    leet timestamp
)

insert into wtf (id) values (15)

select * from wtf

15    0x00000000000007D3 

因此,时间戳是某种二进制数。 如果我们尝试铸造日期时间?

select CAST(leet AS datetime) from wtf

1900-01-01 00:00:06.677

目前一年对我来说是不是1900年。所以,我不知道是什么的SQL Server的想法。



Answer 5:

假设Table 1和表2有三列A,B和时间戳。 我想从表1插入到表2。

这种失败的时间戳错误:

Insert Into Table2
Select Table1.A, Table1.B, Table1.TimeStamp From Table1

这工作:

Insert Into Table2
Select Table1.A, Table1.B, null From Table1


Answer 6:

存在这些问题的答案一些有用的信息。 假设你正在处理,你不能改变的数据库,并且正在从表的一个版本复制到另一个数据,或从同一个表中的一个数据库到另一个。 同时假设有很多的字段,并且您可能需要从所有列的数据,或者你不需要没有默认值的列。 你需要编写一个查询所有的列名。

这里是一个返回所有的表非时间戳列名,您可以剪切并粘贴到您的插入查询的查询。 供参考:189是用于时间戳的类型ID。

declare @TableName nvarchar(50) = 'Product';

select stuff(
    (select 
        ', ' + columns.name
    from 
        (select id from sysobjects where xtype = 'U' and name = @TableName) tables
        inner join syscolumns columns on tables.id = columns.id
    where columns.xtype <> 189
    for xml path('')), 1, 2, '')

就在从“产品”到你的表名顶部更改表的名称。 该查询将返回列名的列表:

ProductID, Name, ProductNumber, MakeFlag, FinishedGoodsFlag, Color, SafetyStockLevel, ReorderPoint, StandardCost, ListPrice, Size, SizeUnitMeasureCode, WeightUnitMeasureCode, Weight, DaysToManufacture, ProductLine, Class, Style, ProductSubcategoryID, ProductModelID, SellStartDate, SellEndDate, DiscontinuedDate, rowguid, ModifiedDate

如果您是从一个数据库(DB1)到另一个数据库(DB2)复制数据,你可以使用此查询。

insert DB2.dbo.Product (ProductID, Name, ProductNumber, MakeFlag, FinishedGoodsFlag, Color, SafetyStockLevel, ReorderPoint, StandardCost, ListPrice, Size, SizeUnitMeasureCode, WeightUnitMeasureCode, Weight, DaysToManufacture, ProductLine, Class, Style, ProductSubcategoryID, ProductModelID, SellStartDate, SellEndDate, DiscontinuedDate, rowguid, ModifiedDate)
select ProductID, Name, ProductNumber, MakeFlag, FinishedGoodsFlag, Color, SafetyStockLevel, ReorderPoint, StandardCost, ListPrice, Size, SizeUnitMeasureCode, WeightUnitMeasureCode, Weight, DaysToManufacture, ProductLine, Class, Style, ProductSubcategoryID, ProductModelID, SellStartDate, SellEndDate, DiscontinuedDate, rowguid, ModifiedDate 
from DB1.dbo.Product


Answer 7:

创建表演示(ID INT,TS时间戳)

插入到演示(ID,TS)的值(1,DEFAULT)



文章来源: SQL Server: Cannot insert an explicit value into a timestamp column