,您可以更新使用條款where id in (select ... connect by...)
或者您可以使用merge
:
merge into htable h
using (select distinct id
from htable
where status <> 'CLOSE'
connect by prior parent_id = id
start with parent_id = 11) src
on (h.id = src.id)
when matched then update set status = 'INACTIVE'
測試數據:
create table htable (id number(4), parent_id number(4), status varchar2(10));
insert into htable values ( 1, null, 'ACTIVE');
insert into htable values ( 11, 1, 'CLOSE');
insert into htable values ( 12, 1, 'ACTIVE');
insert into htable values (111, 11, 'ACTIVE');
insert into htable values (112, 11, 'ACTIVE');
insert into htable values (121, 12, 'ACTIVE');
insert into htable values ( 2, null, 'ACTIVE');
insert into htable values ( 21, 2, 'ACTIVE');
insert into htable values (211, 21, 'ACTIVE');
insert into htable values (212, 21, 'ACTIVE');
後merge
:
ID PARENT_ID STATUS
----- --------- ----------
1 INACTIVE
11 1 INACTIVE
12 1 ACTIVE
111 11 INACTIVE
112 11 INACTIVE
121 12 ACTIVE
2 ACTIVE
21 2 ACTIVE
211 21 ACTIVE
212 21 ACTIVE