Oracle批量添加主键冲突,实现数据不存在则插入,数据存在则更新(insert or update)

此文章为转载文章,仅供学习

转载自:Oracle实现数据不存在则插入,数据存在则更新(insert or update) - 走看看

      思路是写一个函数,先按条件查询数据,假设查询到数据则更新。假设没有查询到数据则插入:

create or replace function fn_merge_index(statdate      in date,
                                          cpid          in varchar2,
                                          indextypecode in number,
                                          indexitemcode in number,
                                          indexdata     in varchar2)
  return number is
  numb number;
begin
  select count(*)
    into numb
    from cp_index_statistics_rec
   where stat_date = to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd')
     and cp_id = cpid
     and index_type_code = indextypecode
     and index_item_code = indexitemcode;
  if numb = 0 then
    --数据不存在,insert
    begin
      insert into cp_index_statistics_rec
        (stat_id,
         stat_date,
         diagnosis,
         cp_id,
         is_validate,
         index_type_code,
         index_item_code,
         stat_data,
         stat_create_date,
         cp_name)
      values
        (cp_index_statistics_rec_seq.nextval,
         to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd'),
         '',
         cpid,
         1,
         indextypecode,
         indexitemcode,
         indexdata,
         (select sysdate from dual),
         (select cp_name from cp_templet_master where cp_id = cpid));
      commit;
    end;
  else
    --数据存在,update
    begin
      update cp_index_statistics_rec
         set is_validate      = 1,
             stat_data        = indexdata,
             stat_create_date =
             (select sysdate from dual)
       where stat_date = to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd')
         and cp_id = cpid
         and index_type_code = indextypecode
         and index_item_code = indexitemcode;
      commit;
    end;
  end if;
  return numb;
end fn_merge_index;

注意to_date(to_char(statdate, 'yyyy/mm/dd'), 'yyyy/mm/dd')这个写法,假设写成to_date(statdate, 'yyyy/mm/dd'),依据NLS不同。可能导致数据出错。详细请看这里

另外oracle提供了merge into能够实现此功能。理论上讲比上面的效率会高。可是没做试验。merge into有个缺点就是在10g下面版本号的oracle中会出现故障,导致比較严重的后果(据说会把全部的数据都更新,而9i又不支持在update后加条件),所以我没有採用这种方法。

merge into的使用方法:

merge into bonuses d 
using (select employee_id, salary, department_id from employees 
where department_id = 80) s 
on (d.employee_id = s.employee_id) 
when matched then update set d.bonus = d.bonus + s.salary*.01 
when not matched then insert (d.employee_id, d.bonus) 
values (s.employee_id, s.salary*0.01); 


另外还有个思路。直接update,运行后会返回受影响的行数。假设行数为0,表示没有符合条件的数据。后面运行insert;假设行数大于0。表示有符合条件的行数且update运行成功。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 MyBatis-Plus 中处理 Oracle 数据库的联合主键数据批量更新,可以按照以下步骤进行操作: 1. 确保你已经定义了对应的实体类,并在实体类中使用 `@TableId` 注解标识联合主键字段。 ```java @Data @TableName("your_table_name") public class YourEntity { @TableId(type = IdType.INPUT) private Long key1; @TableId(type = IdType.INPUT) private Long key2; // 其他属性 } ``` 2. 创建一个 Mapper 接口,继承自 `BaseMapper` 并指定实体类作为泛型参数。 ```java @Repository public interface YourMapper extends BaseMapper<YourEntity> { } ``` 3. 在 Service 层中使用 MyBatis-Plus 提供的方法进行批量更新操作。具体来说,可以使用 `updateBatchById` 方法。 ```java @Service public class YourService { private final YourMapper yourMapper; @Autowired public YourService(YourMapper yourMapper) { this.yourMapper = yourMapper; } public void batchUpdate(List<YourEntity> entityList) { yourMapper.updateBatchById(entityList); } } ``` 4. 在 Oracle 数据库中,由于不支持直接使用 `(key1, key2) in` 的语法,可以通过使用临时表来实现批量更新。首先,创建一个临时表,用于存储批量更新数据。 ```sql CREATE GLOBAL TEMPORARY TABLE temp_table ( key1 NUMBER, key2 NUMBER ) ON COMMIT PRESERVE ROWS; ``` 5. 接下来,在批量更新方法中使用原生 SQL 来执行批量更新操作。 ```java @Repository public interface YourMapper extends BaseMapper<YourEntity> { @Update("INSERT INTO temp_table (key1, key2) VALUES (#{item.key1}, #{item.key2})") void insertIntoTempTable(@Param("item") YourEntity item); @Update("UPDATE your_table_name t SET column1 = ?, column2 = ? WHERE EXISTS (SELECT 1 FROM temp_table WHERE t.key1 = temp_table.key1 AND t.key2 = temp_table.key2)") void batchUpdate(); } ``` 在上述示例中,我们使用 `insertIntoTempTable` 方法将批量更新数据插入临时表中,然后使用 `batchUpdate` 方法执行批量更新操作。在 `batchUpdate` 方法中,我们使用原生 SQL更新目标表的数据,通过与临时表的关联来实现根据联合主键批量更新的效果。 最后,调用 `batchUpdate` 方法即可执行批量更新操作。 请确保在配置文件中配置好数据库连接信息和相关的 MyBatis-Plus 配置。希望对你有所帮助!如果有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值