Oracle兼容-语法-INSERT ALL


1. 语法

  1. INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY]
  2. { ALL insert_into_clause ... | conditional_insert_clause }
  3. {SELECT ... }
  4. insert_into_clause:
  5. INTO tbl_name
  6. [PARTITION (partition_name [, partition_name] ...)]
  7. [(col_name [, col_name] ...)]
  8. [{ VALUES | VALUE} (value_list)]
  9. conditional_insert_clause:
  10. { ALL | FIRST } when_insert_clause ... [ else_insert_clause ]
  11. when_insert_clause:
  12. WHEN condition THEN insert_into_clause ...
  13. else_insert_clause:
  14. ELSE insert_into_clause ...
  15. value:
  16. {expr | DEFAULT}
  17. value_list:
  18. value [, value] ...
  19. EXPLAIN insert_all_stmt

2. 定义和用法

在多表插入中,INSERT ALL INTO支持从子查询计算返回的行派生的计算行插入到一个或多个表中。

  1. 支持插入不同表。

  2. 支持条件查询插入。

INSERT ALL INTO使用限制:

  1. 插入的目标只能是表,不能是视图,不能是集合表达式

  2. 插入结束后,函数 LAST_INSERT_ID() 返回值为0,避免多表插入歧义。

  3. 运行EXPLAIN FORMAT=JSON 查看执行计划的输出结果只会展示插入第一张表。

3. 示例

  1. greatsql> CREATE TABLE t3(id INT PRIMARY KEY, cc VARCHAR(10));
  2. greatsql> CREATE TABLE t2 LIKE t3;
  3. greatsql> CREATE TABLE t1 LIKE t3;
  4. greatsql> INSERT INTO t3 VALUES(1, 'test1'),(2,'test2'), (3,'test3');
  5. greatsql> INSERT ALL
  6. INTO t1
  7. INTO t2
  8. SELECT * FROM t3;
  9. Query OK, 6 rows affected (0.45 sec)
  10. Records: 6 Duplicates: 0 Warnings: 0
  11. greatsql> INSERT ALL
  12. INTO t1(id) VALUES (id+20)
  13. INTO t2(cc,id) VALUES ('test',id+10)
  14. SELECT id FROM t3;
  15. Query OK, 6 rows affected (0.05 sec)
  16. Records: 6 Duplicates: 0 Warnings: 0
  17. greatsql> INSERT ALL
  18. INTO t1 (id,cc) VALUES(13,'test13')
  19. INTO t1 (id,cc) VALUES(12,'test12')
  20. SELECT 1 FROM DUAL;
  21. Query OK, 2 rows affected (0.06 sec)
  22. Records: 2 Duplicates: 0 Warnings: 0
  23. greatsql> SELECT * FROM t2;
  24. +----+-------+
  25. | id | cc |
  26. +----+-------+
  27. | 1 | test1 |
  28. | 2 | test2 |
  29. | 3 | test3 |
  30. | 11 | test |
  31. | 12 | test |
  32. | 13 | test |
  33. +----+-------+
  34. greatsql> SELECT * FROM t1;
  35. +----+--------+
  36. | id | cc |
  37. +----+--------+
  38. | 1 | test1 |
  39. | 2 | test2 |
  40. | 3 | test3 |
  41. | 12 | test12 |
  42. | 13 | test13 |
  43. | 21 | NULL |
  44. | 22 | NULL |
  45. | 23 | NULL |
  46. +----+--------+

问题反馈

联系我们

扫码关注微信公众号

greatsql-wx