Oracle merge constants into single table

后端 未结 3 1549
攒了一身酷
攒了一身酷 2021-02-01 06:25

In Oracle, given a simple data table:

create table data (
    id       VARCHAR2(255),
    key      VARCHAR2(255),
    value    VARCHAR2(511));

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 06:59

    I don't consider using dual to be a hack. To get rid of binding/typing twice, I would do something like:

    merge into data
    using (
        select
            'someid' id,
            'testKey' key,
            'someValue' value
        from
            dual
    ) val on (
        data.id=val.id
        and data.key=val.key
    )
    when matched then 
        update set data.value = val.value 
    when not matched then 
        insert (id, key, value) values (val.id, val.key, val.value);
    

提交回复
热议问题