很多时候我需要将一个大表的数据(我们称之为源)移动到它的克隆(我们称之为目标)。由于尺寸很大,我更喜欢更新插入,而不是全部删除/插入。
为方便起见,我们假设一个名为“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 有什么好处/缺点。