替换现有行以执行合并操作
当您运行此过程中详述的合并操作时,请将所有步骤(但创建和删除临时暂存表除外)放在单个事务中。如果任何步骤失败,事务将回滚。使用单个事务还将减少提交次数,从而节省时间和资源。
通过替换现有行执行合并操作
-
创建暂存表,然后使用要合并的数据填充它,如下面的伪代码所示。
create temp table stage (like target); insert into stage select * from source where source.filter = 'filter_expression'; -
使用与暂存表的内部联接从要更新的目标表中删除行。
将删除和插入操作放在单个事务块中,以便在出现问题时回滚所有内容。
begin transaction; delete from target using stage where target.primarykey = stage.primarykey; -
插入暂存表中的所有行。
insert into target select * from stage; end transaction; -
删除暂存表。
drop table stage;