假设您有带有位置的车辆V1,A以及B带有位置和的X车辆。该消息适用于具有位置、的车辆。V2BQV1AY
对于delete部分你可以使用这个:
delete from t where veh = 'V1' and loc not in ('A', 'Y');
insert为此merge:_
merge into t
using (select 'V1' veh, 'A' loc from dual union all
select 'V1' veh, 'Y' loc from dual) s
on (t.veh = s.veh and t.loc = s.loc)
when not matched then insert values (s.veh, s.loc);
位置A保持不变,B由X删除delete,由Y添加merge。如果表中有更多列,则可以在一个中完成整个操作merge,但我不知道它是否会更快,因为它需要加入源查询。
编辑:
您必须以某种方式将您的“消息”构建到 Oracle 可读的数据结构中。也许消息已经存储在某个表中,也许您可以使用临时结构。你在你的问题中没有明确这一点。我给了你一个关于对偶的例子,因为我必须以某种方式构造语句。For V1(A, B, C)andV3(X, Y)你可以做union all5 次或使用更短的语法:
select 'V1' veh, column_value loc from table(sys.odcivarchar2list('A', 'B', 'C')) union all
select 'V3' veh, column_value loc from table(sys.odcivarchar2list('X', 'Y'))
sys.odcivarchar2list是 Oracle 预定义的类型,您也可以定义自己的 ( create type locations as table of varchar2(100)) 并使用它。
无论如何,delete为每辆车运行两次:
delete from t where veh = `V1` and loc not in ('A', 'B', 'C');
delete from t where veh = `V3` and loc not in ('X', 'Y');
一次merge:
merge into t
using (
select 'V1' veh, column_value loc from table(sys.odcivarchar2list('A', 'B', 'C'))
union all
select 'V3' veh, column_value loc from table(sys.odcivarchar2list('X', 'Y'))) s
on (t.veh = s.veh and t.loc = s.loc)
when not matched then insert values (s.veh, s.loc);