阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

多表insert操作详解

154次阅读
没有评论

共计 2108 个字符,预计需要花费 6 分钟才能阅读完成。

–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

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2108字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中