简体   繁体   English

在 ORACLE 中使用 MERGE 语句删除和插入

[英]DELETE and INSERT using MERGE statement in ORACLE

I have entity Employee, and it has a field List<String> accountIds.我有实体员工,它有一个字段List<String> accountIds.

so table structure looks like this:所以表结构如下所示:

CREATE TABLE EMPLOYEE (
ID varchar2(255) not null,
OBJ_ID varchar2(36), 
NAME varchar2(255),
VER_NBR number(19,0),
CREATEID varchar2(255) not null,
CREATETIME timestamp (6) not null,
UPDATEID varchar2(255),
UPDATETIME timestamp (6),
primary key (ID));

and to store AccountIds I've another table并存储 AccountIds 我有另一个表

CREATE TABLE EMPLOYEE_ACCOUNT_IDS(
        EMP_ID varchar2(255),
        ACC_ID varchar2(255),
        primary key (EMP_ID, ACC_ID)
);

Update operation : ACCOUNT_IDS in EMPLOYEE table更新操作:EMPLOYEE 表中的 ACCOUNT_IDS

Right now, in the application I'm deleting all the accountids related to the employee and re-insert all.现在,在应用程序中,我正在删除与员工相关的所有帐号并重新插入所有帐号。

To improve performance and reduce the number of db queries.提高性能并减少数据库查询次数。 Is this possible to do with "MERGE" STATEMENT.这可能与“合并”声明有关吗?

Yep.是的。 It is possible, not sure if faster then delete and insert.有可能,不确定是否更快然后删除和插入。 I don't know how your input looks like, so it would be nice how do you perform insert.我不知道你的输入是什么样的,所以你如何执行插入会很好。 Anyway you can make join of existing data and new values and use this query as source table.无论如何,您可以连接现有数据和新值,并将此查询用作源表。

merge into employee_account_ids tgt
using (  
  with new_data(acc_id) as (select * from table(sys.odcivarchar2list('NEW01', 'NEW02', 'ACC13')))
    select '001' emp_id, nvl(a.acc_id, n.acc_id) acc_id, rwd,
           case when n.acc_id is null then 'del' end dsc
      from new_data n 
      full join (select e.*, rowid rwd from employee_account_ids e where emp_id = '001') a 
        on a.acc_id = n.acc_id ) src
on (src.emp_id = tgt.emp_id and src.rwd = tgt.rowid)
when matched then update set tgt.acc_id = tgt.acc_id delete where dsc = 'del'
when not matched then insert values (src.emp_id, src.acc_id)

dbfiddle 数据库小提琴

The problem is your second table has no dummy column which I could touch to perform delete, because Oracle requires update first to delete row.问题是您的第二个表没有我可以触摸以执行删除的虚拟列,因为 Oracle 需要先更新才能删除行。 So I used rowid, cheated update ( tgt.acc_id = tgt.acc_id ) and it works.所以我使用了 rowid,欺骗更新( tgt.acc_id = tgt.acc_id )并且它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM