简体   繁体   English

SQL Server更新时间戳列

[英]SQL Server updating a time stamp column

In my database I have a timestamp column. 在我的数据库中,我有一个timestamp列。 I need to update a row in the table and need to update the timestamp column. 我需要更新表中的一行,并需要更新timestamp列。 When I run an update command I get: 当我运行更新命令时,我得到:

Cannot update a timestamp column.

How can I update the timestamp column? 如何更新时间戳列?

You don't 你没有

The timestamp column is updated automatically . 时间戳列会自动更新 Perhaps you are under the impression that timestamp contains a value relating to the time? 也许你的印象是时间戳包含与时间有关的值? It doesn't, but simply is a number which is updated whenever a value in that record is. 它没有,但只是一个数字,只要该记录中的值是更新的数字。 Think of it like a row version number . 可以把它想象成一个行版本号

From MSDN : 来自MSDN

The timestamp data type is just an incrementing number and does not preserve a date or a time. 时间戳数据类型只是一个递增的数字,不保留日期或时间。

You don't update the timestamp column - a timestamp (now renamed rowversion ) column is automatically updated by SQL server whenever any other column in the row is updated. 您不更新timestamp列 - 只要更新行中的任何其他列,SQL Server就会自动更新时间戳(现在重命名的rowversion )列。


If you already knew this, but for some reason want to force the column to update, just perform an update against the row you want affected. 如果您已经知道这一点,但由于某种原因想要强制更新列,只需针对您想要影响的行执行更新。 Even if it results in no actual data change, the timestamp column will still update: 即使它没有导致实际数据更改,时间戳列仍将更新:

create table #T1 (
    ID int not null,
    ts timestamp not null
)
insert into #T1 (ID)
select 1 union all
select 2
select * from #T1
update #T1 set ID = ID where ID=1
select * from #T1

ID  ts
1    0x0000000000039AAF
2    0x0000000000039AB0

ID  ts
1    0x0000000000039AB1
2    0x0000000000039AB0

Although mine is not an answer to your question I wanted to mention how TIMESTAMP can be misunderstood. 虽然我的问题不是我的答案,但我想提一下TIMESTAMP如何被误解。

You don't state your use of the TIMESTAMP column in your question but they fact you are trying to update it implies (to me) you are trying to record when your data changes. 您没有在问题中声明使用TIMESTAMP列,但实际上您尝试更新它意味着(对我而言)您正在尝试记录数据何时发生更改。

Have a look at this article (there are also many others on the net) regarding using TIMESTAMP when you actually want to record the change using a DATETIME . 当您真正想要使用DATETIME记录更改时,请查看本文 (网上还有许多其他内容)关于使用TIMESTAMP

BOL says: BOL说:

The SQL Server timestamp data type has nothing to do with times or dates. SQL Server时间戳数据类型与时间或日期无关。 SQL Server timestamps are binary numbers that indicate the relative sequence in which data modifications took place in a database. SQL Server时间戳是二进制数,表示数据库中数据修改的相对顺序。 The timestamp data type was originally implemented to support the SQL Server recovery algorithms. 最初实现时间戳数据类型以支持SQL Server恢复算法。

As Damien_The_Unbeliever says the type has been renamed and the description says: 正如Damien_The_Unbeliever所说,该类型已被重命名,描述如下:

The rowversion data type is just an incrementing number and does not preserve a date or a time. rowversion数据类型只是一个递增的数字,不保留日期或时间。 To record a date or time, use a datetime2 data type. 要记录日期或时间,请使用datetime2数据类型。

Of course, if you are using the data type in the manner intended ignore all of the above :) 当然,如果你以预期的方式使用数据类型忽略以上所有:)

I had the same problem, my solution: 我有同样的问题,我的解决方案:

UPDATE [table]
SET [ANY_COLUMN] = [ANY_COLUMN]
WHERE ID = [ID]

like anytime when a table value change the timestamp is recalculated, I update any column to the same value therefore the timestamp is updated 就像表值更改时重新计算时间戳一样,我将任何列更新为相同的值,因此更新时间戳

timestamp column cannot be updated since it is server generated and is gauranteed to be unique for the entire database - if updates were allowed, which it is not, then the value is not gauranteed to be unique. timestamp列无法更新,因为它是服务器生成的,并且被保证为整个数据库的唯一性 - 如果允许更新,则不是,那么该值不是唯一的。

i faced same problem trying to update the timestamp column in a replicated instance because the replicated instance did not have the same timestamp value as the publisher - which caused some problems when trying to obtain the last updated record per group of records. 我在尝试更新复制实例中的timestamp列时面临同样的问题,因为复制的实例没有与发布者相同的时间戳值 - 这在尝试获取每组记录的最后更新记录时会导致一些问题。 of course, with replication, it was a simple configurationg at the publisher to enable the same timestamp value to replicate over to the subscriber. 当然,通过复制,发布者只需一个简单的配置即可将相同的时间戳值复制到订阅者。

otherwise, there is no other way to update a timestamp value. 否则,没有其他方法可以更新时间戳值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM