Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle sql合并以插入和删除但不更新_Sql_Oracle_Merge_Insert_Sql Delete - Fatal编程技术网

Oracle sql合并以插入和删除但不更新

Oracle sql合并以插入和删除但不更新,sql,oracle,merge,insert,sql-delete,Sql,Oracle,Merge,Insert,Sql Delete,有没有一种方法可以使用oracle merge来插入和删除而不是更新 我有一个表,表示与另一个表中的一行相关的一组值。我可以通过删除所有值并添加回新的值集,或者有选择地删除一些值并添加其他值来更改值集,但是如果可能的话,我有兴趣将其作为一条语句 下面是一个更新的工作示例。为了实现这一点,我必须添加dummy,这样就可以更新一个列,而该列不处于on状态。有没有办法只删除和插入而不更新虚拟列 更新集列表中不可能有on条件中的列,即使它实际上没有更新 create table every_value

有没有一种方法可以使用oracle merge来插入和删除而不是更新

我有一个表,表示与另一个表中的一行相关的一组值。我可以通过删除所有值并添加回新的值集,或者有选择地删除一些值并添加其他值来更改值集,但是如果可能的话,我有兴趣将其作为一条语句

下面是一个更新的工作示例。为了实现这一点,我必须添加
dummy
,这样就可以更新一个列,而该列不处于
on
状态。有没有办法只删除和插入而不更新虚拟列

更新集
列表中不可能有
on
条件中的列,即使它实际上没有更新

create table every_value ( the_value varchar2(32) );
create table paired_value ( the_id number, a_value varchar2(32) , dummy number default 0 );
-- the_id is a foreign_key to a row in another table

insert into every_value ( the_value ) values ( 'aaa' );
insert into every_value ( the_value ) values ( 'abc' );
insert into every_value ( the_value ) values ( 'ace' );
insert into every_value ( the_value ) values ( 'adg' );
insert into every_value ( the_value ) values ( 'aei' );
insert into every_value ( the_value ) values ( 'afk' );

-- pair ace and afk with id 3
merge into paired_value p using every_value e
on ( p.the_id = 3 and p.a_value = e.the_value )
when matched then update set dummy=dummy+1
delete where a_value not in ('ace','afk')
when not matched then insert (the_id,a_value)
values (3,e.the_value)
where e.the_value in ('ace','afk');

-- pair ace and aei with id 3
-- should remove afk, add aei, do nothing with ace
merge into paired_value p using every_value e
on ( p.the_id = 3 and p.a_value = e.the_value )
when matched then update set dummy = dummy+1
delete where a_value not in ('ace','aei')
when not matched then insert (the_id,a_value)
values (3,e.the_value)
where e.the_value in ('ace','aei');

-- pair aaa and adg with id 4
merge into paired_value p using every_value e
on ( p.the_id = 4 and p.a_value = e.the_value )
when matched then update set dummy = dummy+1
delete where a_value not in ('aaa','adg')
when not matched then insert (the_id,a_value)
values (4,e.the_value)
where e.the_value in ('aaa','adg');

select * from paired_value;

我曾在oracle 10g和oracle 11g中尝试过此操作。

否,您无法删除未通过merge命令更新的行。
以下是文件:

指定DELETE where_子句以在 填充或更新它受此子句影响的行只有 目标表中由合并更新的行 操作。“删除位置”条件计算更新后的值,而不是 更新集计算的原始值。。。哪里 条件如果目标表的一行符合删除条件 条件,但不包括在ON子句定义的联接中, 然后它不会被删除。在目标上定义的任何删除触发器 表将为每行删除激活

这意味着必须更新行。howewer,您不需要更新所有行,更新后使用与删除后相同的WHERE子句

when matched then update set dummy=dummy
    where a_value not in ('ace','afk')
delete 
    where a_value not in ('ace','afk')

我发现您可以将列设置为自身:

MERGE ...
WHEN MATCHED THEN 
   UPDATE SET a_value = a_value WHERE a_value not in ('ace','afk')
   DELETE WHERE a_value not in ('ace','afk')

这就不需要虚拟列

我很确定没有比拥有一个虚拟列更好的方法了,但我不会仅仅为此添加一个虚拟列。哦,好吧,在你的条件下你的价值是多少?在11g中,如果列处于ON条件,即使更新将值设置为自身,我也会收到“ON子句中引用的列无法更新”错误。这是正确的。根据oracle文档,您不能更新作为ON子句一部分的列。如果你仔细想想,这是有道理的。但是您可以在更新中使用不同的非虚拟列;没有尝试过,我不知道delete子句是否仍然会触发相同的错误?例如,设置b_值=b_值,其中a_值不在('ace','afk')中删除a_值不在('ace','afk')中的位置。