oracle多表插入

2021-05-02 22:07:02 字數 3306 閱讀 8100

發表:csdn

日期:20090828

在oracle中關於多表插入的有四種分別是:

1.無條件的多表insert all

2.帶條件的多表insert all

3.帶條件的多表insert first

4.pivoting insert

語法:insert

[all] [first]

[when condition then] [insert_into_clause values_clause]

[else] [insert_into_clause values_clause]

(subquery)

用例子說明,表結構如下:

drop table table_all;

create table table_all(

main varchar2(10),

col1 varchar2(10),

col2 varchar2(10),

col3 varchar2(10),

col4 varchar2(10),

col5 varchar2(10),

col6 varchar2(10));

---插入資料

insert into table_all select  'main1','1','2','3','4','5','6' from dual;

insert into table_all select  'main2','1','2','3','4','5','6' from dual;

insert into table_all select  'main3','1','2','3','4','5','6' from dual;

insert into table_all select  'main4','1','2','3','4','5','6' from dual;

insert into table_all select  'main5','1','2','3','4','5','6' from dual;

insert into table_all select  'main6','1','2','3','4','5','6' from dual;

insert into table_all select  'main7','7','7','7','4','5','6' from dual;

insert into table_all select  'main7','7','7','7','4','5','6' from dual;

insert into table_all select  'main7','7','7','7','4','5','6' from dual;

insert into table_all select  'main7','7','7','7','4','5','6' from dual;

insert into table_all select  'main7','8','8','8','4','5','6' from dual;

---子表

create table table_b as select main,col1,col2,col3 from table_all where 1=0

create table table_a as select main,col4,col5,col6 from table_all where 1=0

create table table_col as select main,col1 col from table_all

where 1=0

create table table_all_1 as select * from table_all where 1=0

create table table_

all_2

as select * from table_all where 1=0

create table table_

all_3

as select * from table_all where 1=0

無條件的多表insert all

insert all

into table_b (main,col1,col2,col3) values (main,col1,col2,col3)

into table_a (main,col4,col5,col6) values (main,col4,col5,col6)

select  main,col1,col2,col3,col4,col5,col6 from table_all;

2.帶條件的多表insert all

insert all

when main = 'main7' then

into table_all_1

when col1 = '8' then

into table_all_2

else

into  table_all_3

select * from table_all

3.帶條件的多表insert first

delete from table_all_1

delete from table_all_2

delete from table_all_3

insert first

when main = 'main7' then

into table_all_1

when col1 = '8' then

into table_all_2

else

into  table_all_3

select * from table_all

insert all與insert first 的區別在與:即如果使用first,當第乙個when條件滿足時,執行第乙個into語句,在後面的條件不再判斷,直接跳到下一行資料.

檢視table_all_2 可以發現:insert all語句執行後 有資料,而在 insert first 語句執行後沒有資料。

4.pivoting insert

insert all

into  table_col values (main,col1)

into  table_col values(main,col2)

into  table_col values(main,col3)

into  table_col values(main,col4)

into  table_col values(main,col5)

into  table_col values(main,col6)

select * from table_all where main='main1'

oracle 多表插入

建立表 create table tb user id integer primary key,user name varchar2 20 not null,user age integer not null create sequence seq user increment by 1 start...

oracle的多表插入

q5,oracle的多表插入操作。在業務處理過程中,經常會碰到將業務資料按照條件分別插入不同的資料表的問題,按照傳統的處理方式,需要分條件執行多次檢索後分別插入不同的表單,這樣因為執行了重複的檢索造成cpu和記憶體的浪費,從oracle9i開始引入了insert all關鍵字支援將某張表的資料同時插...

oracle實現同時多表插入

oracle實現同時多表插入 最簡單的,一條select結果向多個表中插入 insert all into test1 id,name,cj into test2 id,name,cj into test3 id,name,cj select id,name,cj from test 依據條件實現 ...