很多时候,我需要将一个大表(我们称其为源)的数据移动到它的克隆(我们称其为目标)。由于它很大,所以我更喜欢upsert,而不是只删除/插入所有内容。
为了简单起见,让我们假设一个名为"id“的int PK列。
到目前为止,为了做到这一点,我使用了日期时间字段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会有什么好处/缺点。
发布于 2018-09-13 01:45:50
尽管datetime提供了在某些情况下可能有用的信息,但rowversion更可靠,因为系统datetime总是有被更改并失去准确性的风险。在你的例子中,我个人更喜欢rowversion的可靠性。
https://stackoverflow.com/questions/51948437
复制相似问题