很多时候,我需要将一个大表的数据(我们称之为源)移动到它的克隆(我们称之为目标)。由于尺寸较大,我更喜欢向上插入,而不是全部删除/插入。
为了方便起见,我们假设一个名为“id”的int-pk列。
到目前为止,为了做到这一点,我使用了两个表上都存在的日期时间字段dbupddate,它保存了最近插入/更新行的时间。这是通过使用触发器完成的,对于任何插入/更新,触发器都将dbupddate设置为getdate()。
因此,到目前为止,我运行的mill-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时,实际上有哪些好处/缺点。