Oracle将常量合并到单个表中 [英] Oracle merge constants into single table

查看:148
本文介绍了Oracle将常量合并到单个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Oracle中,给出了一个简单的数据表:

In Oracle, given a simple data table:

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

假设我要插入或更新"一个值.我有类似的东西:

suppose I want to "insert or update" a value. I have something like:

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

有没有比这更好的方法了?该命令似乎具有以下缺点:

Is there a better way than this? This command seems to have the following drawbacks:

  • 每个文字都需要键入两次(或通过参数设置添加两次)
  • 使用双重"语法似乎很困难

如果这是最好的方法,那么是否有办法在JDBC中两次设置每个参数?

If this is the best way, is there any way around having to set each parameter twice in JDBC?

推荐答案

我不认为使用Dual是一种hack.为了摆脱两次绑定/键入,我会做类似的事情:

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);

这篇关于Oracle将常量合并到单个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆