如果行不存在,则 Oracle 插入

2023-09-19数据库问题
14

本文介绍了如果行不存在,则 Oracle 插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

insert ignore into table1 
select 'value1',value2 
from table2 
where table2.type = 'ok'

当我运行它时,我收到错误缺少 INTO 关键字".

When I run this I get the error "missing INTO keyword".

推荐答案

当我运行它时,我收到错误缺少 INTO 关键字".

When I run this I get the error "missing INTO keyword" .

因为 IGNORE 不是 Oracle 中的关键字.那是 MySQL 语法.

Because IGNORE is not a keyword in Oracle. That is MySQL syntax.

您可以做的是使用 MERGE.

What you can do is use MERGE.

merge into table1 t1
    using (select 'value1' as value1 ,value2 
           from table2 
           where table2.type = 'ok' ) t2
    on ( t1.value1 = t2.value1)
when not matched then
   insert values (t2.value1, t2.value2)
/

从 Oracle 10g 开始,我们可以在不处理两个分支的情况下使用合并.在 9i 中,我们不得不使用虚拟" MATCHED 分支.

From Oracle 10g we can use merge without handling both branches. In 9i we had to use a "dummy" MATCHED branch.

在更古老的版本中,唯一的选择是:

In more ancient versions the only options were either :

  1. 在发出 INSERT 之前(或在子查询中)测试该行是否存在;
  2. 使用 PL/SQL 执行 INSERT 并处理任何由此产生的 DUP_VAL_ON_INDEX 错误.

这篇关于如果行不存在,则 Oracle 插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12