1

很多时候我需要将一个大表的数据(我们称之为源)移动到它的克隆(我们称之为目标)。由于尺寸很大,我更喜欢更新插入,而不是全部删除/插入。

为方便起见,我们假设一个名为“id”的 int PK col。

到目前为止,为了做到这一点,我使用了两个表上都存在的 datetime 字段 dbupddate,它保存了插入/更新行的最近时间。这是通过使用触发器来完成的,对于任何插入/更新,该触发器将 dbupddate 设置为 getdate()。

因此,到目前为止,我的普通 upsert 代码看起来像:

update t set (col1=s.col1,col2=s.col2 etc)
from source s
inner join target t on s.id=t.id and s.dbupddate>t.dbupddate

insert target 
select * from source s
where not exists (select 1 from target t where t.id=s.id)

最近我偶然发现了rowversion. 我已经阅读并在一定程度上理解了它的功能,但我想知道实际上如果我将 dbupddate 更改为 rowversion 而不是 datetime 有什么好处/缺点。

4

1 回答 1

0

尽管 datetime 包含在某些情况下可能有用的信息,但 rowversion 更可靠,因为系统 datetime 始终存在被更改和失去准确性的风险。就您而言,我个人更喜欢 rowversion 的可靠性。

于 2018-09-12T17:45:50.080 回答