使用子连接将源表合并到目标表中

时间:2015-11-11 14:00:05

标签: sql oracle oracle11g oracle-sqldeveloper

我是SQLDeveloper的新手。请帮我纠正合并语法。我想将act_sl tavle中的数据合并到act_sls_0表中 我在From Clause中使用2个连接。 但我得到错误: 错误(13,13):PL / SQL:ORA-00969:缺少ON关键字 错误(4,1):PL / SQL:忽略SQL语句

merge into act_sls_0 using(
( select * from 
(select a_0.asp, gadim.uic as ga, pmm_styleid, week, colorcode ,  sizecode,      storenum, units_asly,retail_asly,
        cost_asly ,units_asregly,retail_asregly,units_asmkdly,retail_asmkdly,units_as_pln,retail_as_pln
        from act_sls 
        join dimension w on w.externalkey   = 'W'||substr(WEEK,3,2)||'_'||substr(WEEK,5,2)
        join a_0 on ( w.externalkey between startweek and endweek)
        join dimension strdim on strdim.externalkey = (case when length(storenum) <= 4 then RPAD('STR-', 8 - length(storenum), '0') else 'STR-' END) || storenum
        join store_info_0 str on store = strdim.uic
        join dimension gadim on gadim.externalkey = decode(store_type, 'RETAIL', 'GA-01', 'ECOM', 'GA-02', '')
        where trendweeks < 6)t1
        join(select distinct asp, product, ga, color, sstyl_shade, pmm_styleid  from aplc_1 aplc
        join apc_1 apc using (asp,product,color)
        where activeitem = 1 and ga!=0 and color != 0)t2
        on  (t1.asp = t2.asp and 
        t1.pmm_styleid = t2.pmm_styleid
        and (case when length(t1.colorcode) <= 4 then RPAD('SHD-', 8 - length(t1.colorcode), '0') else 'SHD-' END) || t1.colorcode = t2.sstyl_shade
        and t1.ga = t2.ga))src
        on (trg.asp = src.asp and trg.pmm_styleid = src.pmm_styleid and     trg.colorcode = src.colorcode and trg.ga = src.ga) 
      when matched THEN 
 update SET 
when matched THEN 
update SET 
trg.UNITS_ASLY     = src.UNITS_ASLY,
trg.RETAIL_ASLY    = src.RETAIL_ASLY,
trg.COST_ASLY      = src.COST_ASLY,
trg.UNITS_ASREGLY  = src.UNITS_ASREGLY,
trg.RETAIL_ASREGLY = src.RETAIL_ASREGLY,
trg.UNITS_ASMKDLY  = src.UNITS_ASMKDLY,
trg.RETAIL_ASMKDLY = src.RETAIL_ASMKDLY,
trg.UNITS_AS_PLN   = src.UNITS_AS_PLN,
trg.RETAIL_AS_PLN  = src.RETAIL_AS_PLN
 when not matched then insert           
(trg.asp,trg.ga, trg.pmm_styleid,         trg.colorcode,trg.sizecode,trg.storenum,trg.week,trg.UNITS_ASLY,trg.RETAIL_ASLY ,trg.COST_ASLY ,    trg.UNITS_ASREGLY ,trg.RETAIL_ASREGLY ,
  trg.UNITS_ASMKDLY ,trg.RETAIL_ASMKDLY ,trg.UNITS_AS_PLN ,trg.RETAIL_AS_PLN )                                                  
values                                                  
  (src.asp,src.ga, src.pmm_styleid, src.colorcode,src.sizecode,src.storenum,src.week,src.UNITS_ASLY,src.RETAIL_ASLY,src.COST_ASLY,src.UNITS_ASREGLY,
   src.RETAIL_ASREGLY, src.UNITS_ASMKDLY, src.RETAIL_ASMKDLY, src.UNITS_AS_PLN, src.RETAIL_AS_PLN);         

2 个答案:

答案 0 :(得分:0)

合并声明的格式为:

merge into <target_table> tgt
  using <table_name or subquery> src
  on (<join conditions between the target and source datasets>)
when matched then
  update set ...
when not matched then 
  insert (...)
  values (...);

除了你的源子查询不正确之外(t1和t2所在的位置有错误;括号太多,不正确地尝试别名等等),当你在子查询中加入条件时,你&#39 ;完全错过了合并本身的ON子句。

您需要定义与源子查询返回到目标表的数据匹配的连接条件。

答案 1 :(得分:0)

Now the error is : src.ga - Invalid Identifier

虽然我在Trg表和src表中有ga表。

merge into act_sls_0 trg using( select * from 
(select a_0.asp, gadim.uic as ga, pmm_styleid, week, colorcode ,  sizecode,      storenum, units_asly,retail_asly,
        cost_asly ,units_asregly,retail_asregly,units_asmkdly,retail_asmkdly,units_as_pln,retail_as_pln
        from act_sls 
        join dimension w on w.externalkey   = 'W'||substr(WEEK,3,2)||'_'||substr(WEEK,5,2)
        join a_0 on ( w.externalkey between startweek and endweek)
        join dimension strdim on strdim.externalkey = (case when length(storenum) <= 4 then RPAD('STR-', 8 - length(storenum), '0') else 'STR-' END) || storenum
        join store_info_0 str on store = strdim.uic
        join dimension gadim on gadim.externalkey = decode(store_type, 'RETAIL', 'GA-01', 'ECOM', 'GA-02', '')
        where trendweeks < 6)t1
        join(select distinct asp, product, ga, color, sstyl_shade, pmm_styleid  from aplc_1 aplc
        join apc_1 apc using (asp,product,color)
        where activeitem = 1 and ga!=0 and color != 0)t2
        on  (t1.asp = t2.asp and 
        t1.pmm_styleid = t2.pmm_styleid
        and (case when length(t1.colorcode) <= 4 then RPAD('SHD-', 8 - length(t1.colorcode), '0') else 'SHD-' END) || t1.colorcode = t2.sstyl_shade
        and t1.ga = t2.ga))src
        on (trg.asp = src.asp and trg.pmm_styleid = src.pmm_styleid and trg.colorcode = src.colorcode and trg.ga = src.ga)      
when matched THEN 
update SET 
trg.UNITS_ASLY     = src.UNITS_ASLY,
trg.RETAIL_ASLY    = src.RETAIL_ASLY,
trg.COST_ASLY      = src.COST_ASLY,
trg.UNITS_ASREGLY  = src.UNITS_ASREGLY,
trg.RETAIL_ASREGLY = src.RETAIL_ASREGLY,
trg.UNITS_ASMKDLY  = src.UNITS_ASMKDLY,
 trg.RETAIL_ASMKDLY = src.RETAIL_ASMKDLY,
trg.UNITS_AS_PLN   = src.UNITS_AS_PLN,
 trg.RETAIL_AS_PLN  = src.RETAIL_AS_PLN
 when not matched then insert           
 (trg.asp,trg.ga, trg.pmm_styleid,      trg.colorcode,trg.sizecode,trg.storenum,trg.week,trg.UNITS_ASLY,trg.RETAIL_ASLY ,trg.COST_ASLY ,trg.UNITS_ASREGLY ,trg.RETAIL_ASREGLY ,
  trg.UNITS_ASMKDLY ,trg.RETAIL_ASMKDLY ,trg.UNITS_AS_PLN ,trg.RETAIL_AS_PLN )                                                  
values                                                  
  (src.asp,src.ga, src.pmm_styleid, src.colorcode,src.sizecode,src.storenum,src.week,src.UNITS_ASLY,src.RETAIL_ASLY,src.COST_ASLY,src.UNITS_ASREGLY,
   src.RETAIL_ASREGLY, src.UNITS_ASMKDLY, src.RETAIL_ASMKDLY, src.UNITS_AS_PLN, src.RETAIL_AS_PLN);     

提交;