2.1_2 Oracle 批量插入(insert all into)

相关链接

  • 目录

mysql中,批量插入可以这么写:

insert into my_table(field_1,field_2)
values
(value_1,value_2),
(value_1,value_2),
(value_1,value_2);

oracle中不支持这么写,那在Oracle中,怎么通过一个insert语句批量插入数据呢?

INSERT ALL 
INTO my_table(field_1,field_2) VALUES (value_1,value_2) 
INTO my_table(field_1,field_2) VALUES (value_3,value_4) 
INTO my_table(field_1,field_2) VALUES (value_5,value_6)
SELECT 1 FROM DUAL;

补充:评论里提到的为什么要加 SELECT 1 FROM DUAL
官方例子:

INSERT ALL
   INTO sales (prod_id, cust_id, time_id, amount)
   VALUES (product_id, customer_id, weekly_start_date, sales_sun)
   INTO sales (prod_id, cust_id, time_id, amount)
   VALUES (product_id, customer_id, weekly_start_date+1, sales_mon)
   INTO sales (prod_id, cust_id, time_id, amount)
   VALUES (product_id, customer_id, weekly_start_date+2, sales_tue)
   INTO sales (prod_id, cust_id, time_id, amount)
SELECT product_id, customer_id, weekly_start_date, sales_sun,
   sales_mon, sales_tue, sales_wed, sales_thu, sales_fri, sales_sat
   FROM sales_input_table;

个人理解:

“ALL into_clause: Specify ALL followed by multiple insert_into_clauses to perform an unconditional multitable insert. Oracle Database executes each insert_into_clause once for each row returned by the subquery.”

insert all into并不表示一个表中插入多条记录,而是表示多表插入各一条记录,而这多表可以是同一个表,就成了单表插入多条记录。根据后面子查询的结果,前面每条into语句执行一次,博客正文中value都是“字面量”,所以用select 1 from dual返回一条记录即可。


20/08/18

M

你可能感兴趣的:(#,2.1,Oracle,oracle,数据库,database)