0

T1.LEID 需要使用 MSID 和 ESTID 都匹配的行中的 T1.ID 值进行更新。

我已经改变了我的语法并尝试了几种变体,但总是以同样的错误 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 的任何帮助或建议,将不胜感激。

4

2 回答 2

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.
于 2013-06-07T18:28:19.507 回答
0

更新 T1 SET LEID =(从 T2 中选择 T2.id,其中 T2.MSID = T1.MSID 和 T2.ESTID = T1.ESTID);

于 2016-06-29T18:27:12.300 回答