Search code examples
sqloracle-databasesql-updatesubquerysql-insert

when matched then insert in merge


i tried this sql

merge into params t
using (
    select '9158075153713931109' ATTR_ID, '9158310272413033579' OBJECT_ID,
'9158075157713931109' LIST_VALUE_ID
from dual
    where exists (select * from attributes where attr_id = 9158075153713931109)
       ) v
on (t.object_id = v.object_id)
when matched then insert (attr_id, object_id, list_value_id, show_order) values(v.attr_id,v.object_id, v.LIST_VALUE_ID,1);

here i need to insert the value into params only when attr_id (9158075153713931109) exists . i understood only update and delete to be used with "when matched" please help me how can i do this with merge .


Solution

  • I need to insert the value into params only when attr_id (9158075153713931109) exists

    I don't see the point for a merge statement here. As I understand your question, your requirement translates as:

    insert int params (attr_id, object_id, list_value_id, show_order) 
    select s.*, 1
    from (select '9158075153713931109' attr_id, '9158310272413033579' object_id, '9158075157713931109' list_value_id from dual) s 
    where exists (select 1 from attributes a where a.attr_id = s.attr_id)