Oracle 11g - 使用源表中的值更新匹配的行

时间:2013-06-07 18:15:22

标签: sql oracle oracle11g

T1.LEID需要更新,T1.ID值来自 MSID和ESTID匹配的行。

我已经改变了我的语法并尝试了几种变体,但总是会出现相同的错误01427。

UPDATE T1
SET  LEID = (SELECT T2.id
                 FROM T2
                 WHERE   T2.MSID  = T1.MSID )
WHERE EXISTS (SELECT T2.id
          FROM T2
          WHERE T2.ESTID = T1.ESTID
        );

ORA-01427: single-row subquery returns more than one row


 {       CREATE TABLE T1
(
     { 

    ESTID  NUMBER(8),
      MSID   NUMBER(8),
      SMID   NUMBER(8),
      LEID   NUMBER(8)
    )
    NOLOGGING 
    NOCOMPRESS 
    NOCACHE
    NOPARALLEL
    MONITORING;

    Insert into T1
   (ESTID, MSID, SMID, LEID)
    Values
   (545, 5454, 888, NULL);
    Insert into T1
   (ESTID, MSID, SMID, LEID)
    Values
   (334, 5555, 6541, NULL);
    Insert into T1
   (ESTID, MSID, SMID, LEID)
    Values
   (254, 4487, 888, NULL);
    Insert into T1
   (ESTID, MSID, SMID, LEID)   
    Values
   (659, 54518, 654, NULL);
   Insert into T1
   (ESTID, MSID, SMID, LEID)
   Values
   (854, 8548, 85478, NULL); 
    Insert into T1
   (ESTID, MSID, SMID, LEID)
   Values
   (658, 12485, 254, NULL);
    Insert into T1
   (ESTID, MSID, SMID, LEID)
   Values
   (444, 5555, 888, NULL);
    Insert into T1
   (ESTID, MSID, SMID, LEID)
   Values
   (444, 5545, 888, NULL);
    Insert into T1
   (ESTID, MSID, SMID, LEID)
   Values
   (444, 4444, 888, NULL);
   Insert into T1
   (ESTID, MSID, SMID, LEID)
   Values
   (555, 5555, 999, NULL);
   Insert into T1
   (ESTID, MSID, SMID, LEID)
    Values
   (444, 3333, 5, NULL);
      COMMIT; } 

    CREATE TABLE T2 
    (
    ESTID  NUMBER(8),
    MSID   NUMBER(8),
    ID     NUMBER(8)
    )
    NOLOGGING 
    NOCOMPRESS 
    NOCACHE
    NOPARALLEL
    MONITORING;    }

Insert into T2


    {(ESTID, MSID, ID)
    Values
   (545, 5454, 14);
    Insert into T2
   (ESTID, MSID, ID)
   Values
   (334, 5555, 16);
    Insert into T2
   (ESTID, MSID, ID)
   Values
   (254, 4487, 18);
    Insert into T2
    (ESTID, MSID, ID)
    Values
   (659, 54518, 20);
    Insert into T2
   (ESTID, MSID, ID)
   Values
   (854, 8548, 22);
    Insert into T2
   (ESTID, MSID, ID)
    Values
   (658, 12485, 24);
    Insert into T2
   (ESTID, MSID, ID)
   Values
   (444, 5555, 26);
    Insert into T2
   (ESTID, MSID, ID)
   Values
     (444, 5545, 28);
    Insert into T2
   (ESTID, MSID, ID)
   Values
   (444, 4444, 30);
    Insert into T2
   (ESTID, MSID, ID)
    Values
   (555, 5555, 32);
    Insert into T2
   (ESTID, MSID, ID)
    Values
   (444, 3333, 34);
   COMMIT;  }

非常感谢任何有关如何更改明文的帮助或建议更新T1中的ropws。

2 个答案:

答案 0 :(得分:2)

如果两列,MSID和ESTID都需要匹配,以下应该可以解决这个问题:

UPDATE T1
   SET LEID = (SELECT T2.id
                 FROM T2
                WHERE T2.MSID = T1.MSID AND T2.ESTID = T1.ESTID)
 WHERE EXISTS (SELECT T2.id
                 FROM T2
                WHERE T2.MSID = T1.MSID AND T2.ESTID = T1.ESTID);     
11 rows updated.

答案 1 :(得分:0)

更新T1    SET LEID =(选择T2.id                来自T2                WHERE T2.MSID = T1.MSID AND T2.ESTID = T1.ESTID);