oracle |合并数据,其中行与插入的主键不匹配

时间:2017-10-16 20:26:42

标签: oracle merge

我在oracle中有两个用户

  1. nsrtax = target
  2. temp_nsrtax = source
  3. 表的架构是相同的

    Name       Type         
    ---------- ------------  
    CUSTNO     NUMBER(10)                             
    EKRARYEAR  NUMBER(4)                              
    ESALNO     NUMBER(10)                             
    EKRARDATE  DATE                                  
    EKRARFROM  DATE                                  
    EKRARTO    DATE                                  
    TOTALEKRAR NUMBER(15,3)                          
    TOTALSARF  NUMBER(15,3)                          
    EQUAL1     NUMBER(15,3)                          
    DEPTNO     NUMBER(4)                              
    JEBAYA     NUMBER(1)    
    USERIN     NUMBER(3)                             
    USERUP     NUMBER(3)                             
    PRODATE    DATE                                  
    STATE      NUMBER(1)    
    REGNO      NUMBER(2)                              
    BRNO       NUMBER(3)                              
    EKRARNO    NUMBER(4)                              
    EKRARTYPE  NUMBER(1)                              
    EK_LOSS    NUMBER(15,3) 
    TYPESTAXE  NUMBER(10)   
    EXEMPT     NUMBER(10,3) 
    

    两个表中都有主键PR_TAWTEEK primary key (CUSTNO, EKRARYEAR, ESALNO, EKRARTYPE)

    我只是像这样创建合并代码

     MERGE INTO TA_TAWTEEK D    USING (SELECT CUSTNO,EKRARYEAR,ESALNO,EKRARDATE,EKRARFROM,EKRARTO,TOTALEKRAR,TOTALSARF,EQUAL1,DEPTNO,JEBAYA,USERIN,USERUP,PRODATE,STATE,REGNO,BRNO,EKRARNO,EKRARTYPE,EK_LOSS,TYPESTAXE,EXEMPT            
    FROM temp_nsrtax.TA_TAWTEEK 
    WHERE custno=26 ) S  
     ON (D.custno = S.custno) 
    WHEN NOT MATCHED THEN INSERT
     (D.CUSTNO,D.EKRARYEAR,D.ESALNO,D.EKRARDATE,D.EKRARFROM,
     D.EKRARTO,D.TOTALEKRAR,D.TOTALSARF,D.EQUAL1,D.DEPTNO,D.JEBAYA,
     D.USERIN,D.USERUP,D.PRODATE,D.STATE,D.REGNO,D.BRNO,D.EKRARNO,
     D.EKRARTYPE,D.EK_LOSS,D.TYPESTAXE,D.EXEMPT )
    VALUES(S.CUSTNO,S.EKRARYEAR,S.ESALNO,S.EKRARDATE,S.EKRARFROM
     ,S.EKRARTO,S.TOTALEKRAR,
     S.TOTALSARF,S.EQUAL1,S.DEPTNO,S.JEBAYA,S.USERIN,S.USERUP,S.PRODATE,
     S.STATE,S.REGNO,S.BRNO,S.EKRARNO,S.EKRARTYPE,S.EK_LOSS,S.TYPESTAXE,
     S.EXEMPT)S.CUSTNO,S.EKRARYEAR,S.ESALNO,S.EKRARDATE,
     S.EKRARFROM,S.EKRARTO,S.TOTALEKRAR,S.TOTALSARF,S.EQUAL1,
     S.DEPTNO,S.JEBAYA,S.USERIN,S.USERUP,S.PRODATE,S.STATE,S.REGNO,
     S.BRNO,S.EKRARNO,S.EKRARTYPE,S.EK_LOSS,S.TYPESTAXE,S.EXEMPT
    

    但它并不像我需要的那样工作,我需要在主键列不匹配的目标中插入所有行,而不是在所有行未匹配时

    抱歉我的英语不好!

1 个答案:

答案 0 :(得分:2)

两件事。

首先,MATCHEDNOT MATCHED部分与USING ... ON <condition>子句中的条件相对应。因此,如果您想说“不匹配(CUSTNO,EKRARYEAR,ESALNO,EKRARTYPE)”,您需要在连接条件中包含所有这些列,如下所示:

MERGE INTO TA_TAWTEEK D    USING (SELECT CUSTNO,EKRARYEAR,ESALNO,EKRARDATE,EKRARFROM,EKRARTO,TOTALEKRAR,TOTALSARF,EQUAL1,DEPTNO,JEBAYA,USERIN,USERUP,PRODATE,STATE,REGNO,BRNO,EKRARNO,EKRARTYPE,EK_LOSS,TYPESTAXE,EXEMPT            
FROM temp_nsrtax.TA_TAWTEEK 
WHERE custno=26 ) S  
 ON (D.custno = S.custno 
    AND D.EKRARYEAR = S.EKRARYEAR 
    AND D.ESALNO = S.ESALNO
    AND D.EKRARTYPE = S.EKRARTYPE) 
WHEN NOT MATCHED THEN INSERT
 (D.CUSTNO,D.EKRARYEAR,D.ESALNO,D.EKRARDATE,D.EKRARFROM,
 D.EKRARTO,D.TOTALEKRAR,D.TOTALSARF,D.EQUAL1,D.DEPTNO,D.JEBAYA,
 D.USERIN,D.USERUP,D.PRODATE,D.STATE,D.REGNO,D.BRNO,D.EKRARNO,
 D.EKRARTYPE,D.EK_LOSS,D.TYPESTAXE,D.EXEMPT )
VALUES(S.CUSTNO,S.EKRARYEAR,S.ESALNO,S.EKRARDATE,S.EKRARFROM
 ,S.EKRARTO,S.TOTALEKRAR,
 S.TOTALSARF,S.EQUAL1,S.DEPTNO,S.JEBAYA,S.USERIN,S.USERUP,S.PRODATE,
 S.STATE,S.REGNO,S.BRNO,S.EKRARNO,S.EKRARTYPE,S.EK_LOSS,S.TYPESTAXE,
 S.EXEMPT)S.CUSTNO,S.EKRARYEAR,S.ESALNO,S.EKRARDATE,
 S.EKRARFROM,S.EKRARTO,S.TOTALEKRAR,S.TOTALSARF,S.EQUAL1,
 S.DEPTNO,S.JEBAYA,S.USERIN,S.USERUP,S.PRODATE,S.STATE,S.REGNO,
 S.BRNO,S.EKRARNO,S.EKRARTYPE,S.EK_LOSS,S.TYPESTAXE,S.EXEMPT

其次,如果你只是在进行INSERT,那么根本不需要合并,语法更简单:

INSERT INTO TA_TAWTEEK (CUSTNO,EKRARYEAR,ESALNO,EKRARDATE,EKRARFROM,
 EKRARTO,TOTALEKRAR,TOTALSARF,EQUAL1,DEPTNO,JEBAYA,
 USERIN,USERUP,PRODATE,STATE,REGNO,BRNO,EKRARNO,
 EKRARTYPE,EK_LOSS,TYPESTAXE,EXEMPT )
SELECT CUSTNO,EKRARYEAR,ESALNO,EKRARDATE,EKRARFROM,
 EKRARTO,TOTALEKRAR,TOTALSARF,EQUAL1,DEPTNO,JEBAYA,
 USERIN,USERUP,PRODATE,STATE,REGNO,BRNO,EKRARNO,
 EKRARTYPE,EK_LOSS,TYPESTAXE,EXEMPT            
FROM temp_nsrtax.TA_TAWTEEK S
WHERE custno=26  
 AND NOT EXISTS (select 1 from TA_TAWTEEK D where D.custno = S.custno 
    AND D.EKRARYEAR = S.EKRARYEAR 
    AND D.ESALNO = S.ESALNO
    AND D.EKRARTYPE = S.EKRARTYPE)
;