我见过很多答案,当表2中存在行时会更新表1,但没有一个在选择表中存在行时使用LEFT JOIN行(为了更好的性能)。我有一个更新的解决方案,但它会执行不好,因为它使用NOT IN。Oracle在表1中更新列时,当表2中不存在相关行时
因此,这个SQL将根据需要更新表,但在对大型表运行时使用起来非常昂贵。
update header
set status='Z'
where status='A'
and header.id not in (
select headerid
from detail
where detail.id between 0 and 9999999
);
现在我有使用LEFT JOIN返回正确的ID表现良好的查询,但我一直无法将其插入到一个更新语句给予同样的结果。 select语句是
select header.id
from header
left join detail on detail.headerid = header.id
where detail.headerid is null
and header.status='A'
所以,如果我用这个更新语句,如:
update header
set status = 'Z'
where header.id = (
select header.id
from header
left join detail on detail.headerid = header.id
where detail.headerid is null and header.status='A'
)
然后我会失败:
ORA-01427:单行子查询返回多行
我期待着他多ader.id被返回并且想要更新所有这些行。
因此,我仍然在寻找一种解决方案,它将更新返回的行,使用性能良好的SQL select来返回表头中的行,但在详细信息表中没有相关的行。
任何帮助,将不胜感激,否则我将留下性能不佳的更新。