Oracle SQL——用一个语句将多行插入到一个表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10197407/
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
Oracle SQL -- insert multiple rows into a table with one statement?
提问by Jeremy
I'd like to do insert N rows that are all identical, except one of the values is different. Specifically, this is what I am trying:
我想插入 N 行,这些行都相同,但其中一个值不同。具体来说,这就是我正在尝试的:
insert into attribute_list (id,value,name)
values
(
select (id,'Y','is_leveled') from value_list where val >= 50
);
So for every value that has a val >= 50, I would insert one row into attribute_list. Can this be done with one insert statement or should I just manually generate these inserts in excel?
因此,对于 val >= 50 的每个值,我都会在 attribute_list 中插入一行。这可以用一个插入语句来完成,还是我应该在 excel 中手动生成这些插入?
(note: this is a cooked example simplified to clarify the issue, so no need to attack the needlessness of this specific case)
(注意:这是为了澄清问题而简化的成熟示例,因此无需攻击此特定案例的不必要性)
回答by mwigdahl
You can absolutely do this in a single statement!
您绝对可以在一个语句中完成此操作!
Try this:
尝试这个:
INSERT INTO attribute_list (id, value, name)
SELECT id, 'Y', 'is_leveled'
FROM value_list WHERE val >= 50
回答by nes1983
回答by Pavel Veller
You need an INSERT from SELECT. To do so you should omit the VALUES
and just do:
您需要从 SELECT 插入。为此,您应该省略VALUES
并且只执行以下操作:
insert into attribute_list (id,value,name)
select (id,'Y','is_leveled') from value_list where val >= 50;
Here's an example: http://psoug.org/reference/insert.html
回答by GavinCattell
You can do a SELECT ... INTO
if you can query the data. Otherwise to create data you'd need to use PL/SQL
SELECT ... INTO
如果可以查询数据,则可以执行 a 。否则要创建数据,您需要使用 PL/SQL
If you have the data then try:
如果您有数据,请尝试:
select id,'Y','is_leveled'
INTO attribute_list
from value_list where val >= 50