oracle 如何在Oracle中有条件插入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41849762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:22:31  来源:igfitidea点击:

How to insert conditionally in Oracle?

sqloracle

提问by Kamil

I've read herethat the syntax looks like this:

我在这里读到语法如下所示:

INSERT
WHEN ([Condition]) THEN
INTO [TableName] ([ColumnName])
VALUES ([VALUES])
ELSE
INTO [TableName] ([ColumnName])
VALUES ([VALUES])
SELECT [ColumnName] FROM [TableName];

But I don't want to provide values from another table. I just want to type them, so I've got:

但我不想提供另一个表中的值。我只想输入它们,所以我有:

INSERT 
WHEN EXISTS (SELECT 1 FROM FOO WHERE NAME = 'JOE') 
THEN
INTO BAR (NAME, AGE) 
VALUES ('JOE', 50)

and this produces exception: ORA-00928: missing SELECT keyword.

这会产生异常:ORA-00928:缺少 SELECT 关键字。

I want to perform an insert if given value is found in another table.

如果在另一个表中找到给定值,我想执行插入。

采纳答案by hkutluay

using with select also running. but there is a problem with values keyword

与 select 一起使用也在运行。但是 values 关键字有问题

INSERT 
WHEN EXISTS (SELECT 1 FROM FOO WHERE NAME = 'JOE') 
THEN
INTO BAR (NAME, AGE) 
SELECT 'JOE', 50 FROM DUAL

回答by Kamil

So, I've found an indirect way hereand solution for my question would be:

所以,我在这里找到了一种间接的方法,我的问题的解决方案是:

INSERT INTO BAR (NAME, AGE) 
SELECT 'JOE', 50
  FROM DUAL
 WHERE EXISTS (SELECT 1 FROM FOO WHERE NAME = 'JOE')

but it doesn't explain why I have to use SELECTstatement in INSERT WHEN

但它并没有解释为什么我必须SELECTINSERT WHEN