SQL Server:无法在时间戳列中插入显式值

SQL Server:无法在时间戳列中插入显式值,sql,sql-server-2008,insert,timestamp,Sql,Sql Server 2008,Insert,Timestamp,使用此语句时 create table demo ( ts timestamp ) insert into demo select current_timestamp 我得到以下错误: 无法在时间戳列中插入显式值。在列列表中使用INSERT可排除timestamp列,或在timestamp列中插入默认值 如何将当前时间插入时间戳列?,时间戳 是一种公开自动生成的唯一二进制文件的数据类型 数据库中的数字。时间戳通常用作一种机制 用于打印表格行的版本。存储大小为8字节。这个 时间戳数据类

使用此语句时

create table demo (
    ts timestamp
)

insert into demo select current_timestamp
我得到以下错误:

无法在时间戳列中插入显式值。在列列表中使用INSERT可排除timestamp列,或在timestamp列中插入默认值

如何将当前时间插入
时间戳
列?

时间戳

是一种公开自动生成的唯一二进制文件的数据类型 数据库中的数字。时间戳通常用作一种机制 用于打印表格行的版本。存储大小为8字节。这个 时间戳数据类型只是一个递增的数字,而不是 保留日期或时间。要记录日期或时间,请使用datetime 数据类型


您可能正在寻找
datetime
数据类型。

您无法将值显式插入到timestamp列中。它是自动生成的。不要在insert语句中使用此列。有关更多详细信息,请参阅

您可以使用datetime而不是像这样的时间戳:

create table demo (
    ts datetime
)

insert into demo select current_timestamp

select ts from demo
返回:

2014-04-04 09:20:01.153

假设表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

如果需要复制完全相同的时间戳数据,请将目标表中的数据类型从时间戳更改为二进制(8)——我使用了varbinary(8),效果很好

这显然会破坏目标表中的任何时间戳功能,因此请首先确保您对此没有问题。

如何使用SQL Server将当前时间插入时间戳: 在较新版本的SQL Server中,
时间戳
重命名为
行版本
。这是正确的,因为时间戳名称具有误导性

SQL Server的
时间戳
不是由用户设置的,并且不表示日期或时间。时间戳仅用于确保行在被读取后未发生更改

如果要存储日期或时间,请不要使用时间戳,必须使用其他数据类型之一,例如
datetime
smalldatetime
date
time
DATETIME2

例如:

create table wtf (
    id INT,
    leet timestamp
)

insert into wtf (id) values (15)

select * from wtf

15    0x00000000000007D3 
mssql中的“timestamp”是某种内部数据类型。将该数字转换为datetime会产生一个无意义的数字。

创建表演示( id int, ts时间戳 )

插入演示(id,ts)
值(1,默认值)

这些答案中有一些很好的信息。假设您正在处理无法更改的数据库,并且正在将数据从一个版本的表复制到另一个版本,或者从一个数据库中的同一个表复制到另一个版本。假设还有很多列,您需要所有列的数据,或者不需要的列没有默认值。您需要编写一个包含所有列名的查询

下面是一个查询,它返回表的所有非时间戳列名,您可以将这些列名剪切并粘贴到insert查询中。仅供参考: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, '')
如果要将数据从一个数据库(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

可能重复的谢谢!这在创建归档数据库以在一定时间后将内容移动到其中时尤其有用。在存储过程中,由于该错误,我必须将列表中的每一列都放进去,但是现在我可以用*替换它,所以我不需要每次向两个数据库中的匹配表中添加列时都修改我的过程。如果需要对部分结果使用表变量,并且希望原始时间戳包含在最终结果中,这一点也很有用。这是一个非常好的答案。我在存储过程中有一个表变量,必须在返回该表之前将select查询的结果插入该表,将表变量中的列类型更改为[varbinary](8)为我解决了这个问题。这个答案至少确认了我发现的部分错误消息,虽然它没有真正回答用户的问题(我不是那个否决答案的人)。完美的肯特。我在找这样的东西。谢谢:)
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