你好,游客 登录 注册 搜索
背景:
阅读新闻

多表insert操作详解

[日期:2017-12-08] 来源:Linux社区  作者:huangbiquan [字体: ]

--1.无条件的多表insert all
create table emp_1 as select id,last_name from s_emp where 1=0;
create table emp_2 as select * from s_emp where 1=0;
create table emp_3 as select * from s_emp where 1=0;
--没有条件,向多个目标表全量插入,必须有all
insert all
--不指定emp_1后面的列,也不指定values,那么emp_1中的所有列类型和顺序与查询的列的类型和顺序一致
--也就是emp_1中只有查询结果中的那几列,而且类型和顺序与其一致
into emp_1
--指定了emp_2后面的列,没有values,表示emp_2中要插入的列被选择出来,与查询的结果列类型和顺序一致
--emp_2中也可能有很多列,不止这两列
into emp_2(id,last_name)
--指定emp_3后面的列,也指定values,那么values后面的列名必须与查询结果一致,如果
--查询中有别名,必须在values中使用别名。emp_3中指定的列类型和顺序必须与values保持一致
--emp_3中也可能列数大于指定的列数
into emp_3(id,last_name) values(s_id,s_last_name)
select id s_id,last_name s_last_name
  from s_emp;

--2.带条件的多表insert all
--conditional insert all:all可以省略,但不建议.
insert all
--将查询结果中为s_id>20的插入,条件中指定的列必须与查询的结果名字一致,如果有别名,用别名
when s_id>20 then
into emp_1
--s_last_name为M开头的插入,可能插入的行与s_id>20有重复
when s_last_name like 'M%'then
into emp_2(id,last_name)
--如果指定else,则不满足上面两个条件的插入到emp_3,插入的行不会与上面两个重复
else
into emp_3(id,last_name) values(s_id,s_last_name)
select id s_id,last_name s_last_name
  from s_emp;
 
--3.带条件的多表insert first
--Insert first只有带条件的,没有不带条件的。
--语法只要将insert all中的all改为first就可以了。这里的first不可以省略。省略那么默认就是all
insert first
--将查询结果中为s_id>20的插入,条件中指定的列必须与查询的结果名字一致,如果有别名,用别名
when s_id>20 then
into emp_1
--s_last_name为M开头的插入,插入的行与s_id>20没有重复
when s_last_name like 'M%'then
into emp_2(id,last_name)
--如果指定else,则不满足上面两个条件的插入到emp_3,插入的行不会与上面两个重复
else
into emp_3(id,last_name) values(s_id,s_last_name)
select id s_id,last_name s_last_name
from s_emp;

--4.选择插入Pivoting insert
--使用pivoting insert实现将非关系性表记录转换为关系型表中存储。Pivot旋转是OLAP中的一个基本改变,提供多维度数据分析。
insert all
into sales_info values(employee_id,week_id,sales_mon) --分别按每个工作日插入
into sales_info values(employee_id,week_id,sales_tue)
into sales_info values(employee_id,week_id,sales_wed)
into sales_info values(employee_id,week_id,sales_thur)
into sales_info values(employee_id,week_id,sales_fri)
select employee_id,week_id,sales_mon,sales_tue,sales_wed,sales_thur,sales_fri
from sales_source_data;

多表insert使用限制:
1. 只能对table使用多表insert,不能对视图或物化视图使用。
2. 不能对远程表进行这个插入操作。
3. 在做多表insert操作,不能指定一个表的集合表达式。
4. 多表insert中的的into目标表加在一起的列数不能超过999 个。

更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-12/149251.htm

linux
相关资讯       多表insert操作  insert操作 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款