sql - How to use ORACLE MERGE statement to INSERT values using DECODE? -


i'm trying use merge upsert. update code works (i haven't posted here) merge statement insert giving following errors:

  • ora-00963 ---missing expression (when select mentioned in insert-values)
  • ora-00917 ---missing comma (when select removed)
  • when format code, calls syntax check near values & points towards clause.

basically want write merge-insert statement accommodates decode statement well. please me i'm new oracle.

merge table1 t1    using(select distinct a_cd, f_str, a_pm, a_type          table2) t2      on(t1.c_name=t2.a_cd) when not matched   insert(c_type, c_name, c_value)   values (select t3.c_type, t4.a_cd c_name,         decode(t3.c_type,                'a comp', t4.f_str,                'a_pm', t4.a_pm,                'a_type' t4.a_type) c_value)  from(select 'a_comp' c_type dual     union     select 'a_pm' c_type dual     union     select 'a_type' c_type dual)t3,    (select distinct a_cd, f_str, a_pm, a_type     table2 t2     a_cd || f_str || a_pm || a_type     not null     , not exists     (select null     table1 t1     t2.a_cd=t1.c_name))t4; 

you can't use select statement while inserting using merge,so values have use select statement after 'using' keyword , passing value of select insert statement .

  merge table1 t1   using (select t3.c_type, t4.a_cd c_name,       decode(t3.c_type,        'a comp', t4.f_str,        'a_pm', t4.a_pm,        'a_type' t4.a_type) c_value)     from(select 'a_comp' c_type dual     union      select 'a_pm' c_type dual      union      select 'a_type' c_type dual)t3,     (select distinct a_cd, f_str, a_pm, a_type       table2 t2       a_cd || f_str || a_pm || a_type       not null        , not exists       (select null       table1 t1         t2.a_cd=t1.c_name))t4    on(t1.c_name=t4.a_cd)   when not matched      insert(c_type, c_name, c_value)   values(t4.c_type, t4.c_name,t4.c_value); 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -