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

求一UPDATE语句
CREATE TABLE AA (REPORT_M_ID INTEGER,ITEM_ID VARCHAR2(10),YEAR VARCHAR(4),QQ INTEGER ,NEXT_REPORT_M_ID INTEGER,ORG_CODE VARCHAR2(50))

INSERT INTO AA SELECT 1,'A001_01','2012',3,NULL,'DLR1' FROM DUAL;
INSERT INTO AA SELECT 2,'A001_01','2012',5,NULL,'DLR2' FROM DUAL;
INSERT INTO AA SELECT 3,'A001_01','2012',8,NULL,'DLR3' FROM DUAL;

INSERT INTO AA SELECT 4,'A001_01','2012',16,NULL,'AREA1' FROM DUAL;

CREATE TABLE KK (ORG_CODE VARCHAR2(20),PARENT_CODE VARCHAR2(20))/*组织结构*/
INSERT INTO KK SELECT 'AREA1','' FROM DUAL;
INSERT INTO KK SELECT 'DLR1','AREA1' FROM DUAL;
INSERT INTO KK SELECT 'DLR2','AREA1' FROM DUAL;
INSERT INTO KK SELECT 'DLR3','AREA1' FROM DUAL;

AREA1下有3个子节点,DLR1,DLR2,DLR3
求一UPDATE语句:将'AREA1'的REPORT_M_ID更新到DLR1,DLR2,DLR3的NEXT_REPORT_M_ID
条件:WHERE REPORT_M_ID=4(因为我目前只知道AREA1的REPORT_M_ID是等于4)
结果:DLR1,DLR2,DLR3的NEXT_REPORT_M_ID的值更新为4


------解决方案--------------------
SQL code
update aa set NEXT_REPORT_M_ID=(
  select NEXT_REPORT_M_ID from (
    select aa.REPORT_M_ID NEXT_REPORT_M_ID, t.REPORT_M_ID from aa,
           (select aa.*, kk.PARENT_CODE from aa left join kk on aa.ORG_CODE = kk.ORG_CODE) t
    where aa.ORG_CODE = t.PARENT_CODE
  ) tmp
  where aa.REPORT_M_ID=tmp.REPORT_M_ID
);