insert all 学习

来源:互联网 发布:绵阳师范网络教学 编辑:程序博客网 时间:2024/05/01 22:41

 insert all结构的作用是想几个表中同时插入数据。

下面是一个简单的例子。
我们先来使用create select 创建两个表
,这两个表的结构和scott.emp的结构一模一样。如果你对create select
create table insert_all_test1
as
select empno,ename from scott.emp where 1 = 0

create table insert_all_test2
as
select empno,sal from scott.emp where 1 = 0;

然后我们使用insert all,向这两个表插入数据:
insert all
into insert_all_test1 values(no,name)
into insert_all_test2 values(no,sal)
select empno no,ename name,sal from scott.emp;

这里很简单就像两个表插入了数据,虽然最终插入到表中的数据在列上有所区分,但是插入到这两个表的数据的来源是一样的。

insert all结构还有一个用法就是使用when then选择结构来使对应的数据插入到对应的表中,这其实也很简单。
例如:
insert all
when sal > 2000 then
into insert_all_test1 values(no,name)
when sal < 2500 then
into insert_all_test2 values(no,sal)
select empno no,ename name,sal from scott.emp;
这里使用了sal 作为判断条件来将不同的条件数据插入到不同的表中。

原创粉丝点击