日期:2014-05-17  浏览次数:20858 次

获得所有子节点,再UPDATE所有子节点的其中一列
通过GROUP1(即REPORT_M_ID=8)的NEXT_REPORT_M_ID,找出NEXT_REPORT_M_ID下的所有子节点,再把所有子节点的PENDING_ID都修改为GROUP1的PENDING_ID
结果是PENDING_ID都为22
CREATE TABLE DD (REPORT_M_ID INTEGER,ITEM_ID VARCHAR2(10),YEAR VARCHAR(4),QQ INTEGER ,NEXT_REPORT_M_ID INTEGER,ORG_CODE VARCHAR2(50),PENDING_ID INTEGER)

INSERT INTO DD SELECT 1,'A001_01','2012',3,4,'DLR1','' FROM DUAL;
INSERT INTO DD SELECT 2,'A001_01','2012',5,4,'DLR2','' FROM DUAL;
INSERT INTO DD SELECT 3,'A001_01','2012',8,4,'DLR3','' FROM DUAL;
INSERT INTO DD SELECT 4,'A001_01','2012',16,8,'AREA1','' FROM DUAL;
INSERT INTO DD SELECT 5,'A001_01','2012',2,7,'DLR4','' FROM DUAL;
INSERT INTO DD SELECT 6,'A001_01','2012',2,7,'DLR5','' FROM DUAL;
INSERT INTO DD SELECT 7,'A001_01','2012',4,8,'AREA2','' FROM DUAL;
INSERT INTO DD SELECT 8,'A001_01','2012',20,NULL,'GROUP1','22' FROM DUAL;

------解决方案--------------------
SQL code

update DD
set NEXT_REPORT_M_ID=(select root from
    (select CONNECT_BY_ROOT REPORT_M_ID root,REPORT_M_ID
    from DD 
    start with NEXT_REPORT_M_ID is null--或按你说的写这个条件,一样的:ORG_CODE='GROUP1'
    connect by prior REPORT_M_ID=NEXT_REPORT_M_ID) t
    where t.REPORT_M_ID=DD.REPORT_M_ID);

------解决方案--------------------
SQL code
update DD
set PENDING_ID=(
select root from
    (select CONNECT_BY_ROOT PENDING_ID root,REPORT_M_ID
    from DD 
    start with NEXT_REPORT_M_ID is null
    connect by prior REPORT_M_ID=NEXT_REPORT_M_ID) t
where t.REPORT_M_ID=DD.REPORT_M_ID
);