5

我正在使用 Spring JDBC 和 oracle SQL。

使用 SpringJDBC 类 MapSqlParameterSource,我已经映射了我想要合并的数据。

现在我想使用合并语句来更新/插入数据库表。我所拥有的只是一张表和一堆我想合并到其中的参数。

 merge into proj.person_registry pr
using ( ! parameters should go here somehow? )
on (pr.id = :id or pr.code = :code)
when matched then 
update set pr.code             = :code,
        pr.name                 = :name,
        pr.firstname            = :firstname,
        pr.cl_gender            = :cl_gender,
        pr.cl_status            = :cl_status,
        pr.aadress              = :aadress,
        pr.aadress_date         = :aadress_date 
when not matched then
insert values (:code, :name, :firstname, :cl_gender, :cl_status, ;aadress, :aadress_date);

我是否必须以某种方式为using关键字创建一个临时表, 还是有其他方法?我将如何进行这样的合并?

还有两个唯一键 pr.id 和 pr.code。有时参数:idnull,当发生这种情况时,我想根据 pr.code 与 :code 匹配来访问更新语句。如果我的更新包含以下行,它是否仍然有效:

update set pr.code             = :code,
4

1 回答 1

7

这应该有效:

merge into proj.person_registry pr
using ( 
  select 42 as id
         'xyz' as code,
         'Dent' as name,
         'Arthur' as firstname,
         'male' as cl_gender
         'closed' as cl_status,
         'Somewher' as aaddress,
         current_date as aaddress_date
   from dual
) t on (pr.id = t.id or pr.code = t.code)
when matched then 
update set pr.code             = t.code,
        pr.name                 = t.name,
        pr.firstname            = t.firstname,
        pr.cl_gender            = t.cl_gender,
        pr.cl_status            = t.cl_status,
        pr.aadress              = t.aadress,
        pr.aadress_date         = t.aadress_date 
when not matched then
insert values (t.code, t.name, t.firstname, t.cl_gender, t.cl_status, ;aadress, t.aadress_date);

我不熟悉 Spring 的 JDBC 模板,但是用select ... from dual参数占位符替换查询中的实际值应该可以。

于 2012-04-13T17:14:36.847 回答