【发布时间】:2011-03-10 00:56:05
【问题描述】:
insert ignore into table1
select 'value1',value2
from table2
where table2.type = 'ok'
当我运行它时,我收到错误“缺少 INTO 关键字”。
【问题讨论】:
insert ignore into table1
select 'value1',value2
from table2
where table2.type = 'ok'
当我运行它时,我收到错误“缺少 INTO 关键字”。
【问题讨论】:
请注意,如果您有幸使用 11g 第 2 版,您可以使用提示 IGNORE_ROW_ON_DUPKEY_INDEX。
INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX(table1(id)) */ INTO table1 SELECT ...
来自文档: http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/sql_elements006.htm#CHDEGDDG
我博客中的一个例子: http://rwijk.blogspot.com/2009/10/three-new-hints.html
问候, 抢。
【讨论】:
当我运行它时,我收到错误“缺少 INTO 关键字”。
因为 IGNORE 在 Oracle 中不是关键字。那是 MySQL 的语法。
你可以做的是使用 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 分支。
在更古老的版本中,唯一的选择是:
【讨论】:
因为您在“insert”和“into”之间输入了虚假词“ignore”!!
insert ignore into table1 select 'value1',value2 from table2 where table2.type = 'ok'
应该是:
insert into table1 select 'value1',value2 from table2 where table2.type = 'ok'
从您的问题标题“如果行不存在,oracle 插入”我假设您认为“忽略”是一个 Oracle 关键字,意思是“如果它已经存在,请不要尝试插入行”。也许这适用于其他一些 DBMS,但它不适用于 Oracle。您可以使用 MERGE 语句,或像这样检查是否存在:
insert into table1
select 'value1',value2 from table2
where table2.type = 'ok'
and not exists (select null from table1
where col1 = 'value1'
and col2 = table2.value2
);
【讨论】: