好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Oracle 数据仓库ETL技术之多表插入语句的示例详解

大家好!我是只谈技术不剪发的 tony 老师。

etl (提取、转换、加载)是指从源系统中提取数据并将其放入 数据仓库 的过程。oracle 数据库为 etl 流程提供了丰富的功能,今天我们就给大家介绍一下 oracle 多表插入语句,也就是 insert all 语句。

创建示例表

 

我们首先创建一个源数据表和三个目标表:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

create table src_table(

   id integer not null primary key ,

   name varchar2(10) not null

);

insert into src_table values (1, '张三' );

insert into src_table values (2, '李四' );

insert into src_table values (3, '王五' );

 

create table tgt_t1 as

select * from src_table where 1=0;

 

create table tgt_t2 as

select * from src_table where 1=0;

 

create table tgt_t3 as

select * from src_table where 1=0;

无条件的 insert all 语句

 

insert all 语句可以用于将多行输入插入一个或者多个表中,因此也被称为多表插入语句。第一种形式的 insert all 语句是无条件的插入语句,源数据中的每一行数据都会被插入到每个目标表中。例如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

insert all

   into tgt_t1(id, name ) values (id, name )

   into tgt_t2(id, name ) values (id, name )

   into tgt_t3(id, name ) values (id, name )

select * from src_table;

 

select * from tgt_t1;

id| name   |

--|------|

  1|张三  |

  2|李四  |

  3|王五  |

 

select * from tgt_t2;

id| name   |

--|------|

  1|张三  |

  2|李四  |

  3|王五  |

 

select * from tgt_t3;

id| name   |

--|------|

  1|张三  |

  2|李四  |

  3|王五  |

执行以上多表插入语句之后,三个目标表中都生成了 3 条记录。

我们也可以多次插入相同的表,实现一个插入语句插入多行数据的效果。例如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

truncate table tgt_t1;

 

insert all

   into tgt_t1(id, name ) values (4, '赵六' )

   into tgt_t1(id, name ) values (5, '孙七' )

   into tgt_t1(id, name ) values (6, '周八' )

select 1 from dual;

 

select * from tgt_t1;

id| name   |

--|------|

  4|赵六  |

  5|孙七  |

  6|周八  |

在以上插入语句中,tgt_t1 出现了三次,最终在该表中插入了 3 条记录。这种语法和其他数据库中的以下多行插入语句效果相同:

?

1

2

3

-- mysql、sql server、postgresql以及sqlite

insert into tgt_t1(id, name )

values (4, '赵六' ), (5, '孙七' ), (6, '周八' );

另外,这种无条件的 insert all 语句还可以实现列转行(pivot)的功能。例如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

create table src_pivot(

   id integer not null primary key ,

   name1 varchar2(10) not null ,

   name2 varchar2(10) not null ,

   name3 varchar2(10) not null

);

insert into src_pivot values (1, '张三' , '李四' , '王五' );

 

truncate table tgt_t1;

 

insert all

   into tgt_t1(id, name ) values (id, name1)

   into tgt_t1(id, name ) values (id, name2)

   into tgt_t1(id, name ) values (id, name3)

select * from src_pivot;

 

select * from tgt_t1;

id| name   |

--|------|

  1|张三  |

  1|李四  |

  1|王五  |

src_pivot 表中包含了 3 个名字字段,我们通过 insert all 语句将其转换 3 行记录。

有条件的 insert all 语句

 

第一种形式的 insert all 语句是有条件的插入语句,可以将满足不同条件的数据插入不同的表中。例如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

truncate table tgt_t1;

truncate table tgt_t2;

truncate table tgt_t3;

 

insert all

   when id <= 1 then

     into tgt_t1(id, name ) values (id, name )

   when id between 1 and 2 then

     into tgt_t2(id, name ) values (id, name )

   else

     into tgt_t3(id, name ) values (id, name )

select * from src_table;

 

select * from tgt_t1;

id| name   |

--|------|

  1|张三  |

 

select * from tgt_t2;

id| name   |

--|------|

  1|张三  |

  2|李四  |

 

select * from tgt_t3;

id| name   |

--|------|

  3|王五  |

tgt_t1 中插入了 1 条数据,因为 id 小于等于 1 的记录只有 1 个。tgt_t2 中插入了 2 条数据,包括 id 等于 1 的记录。也就是说,前面的 when 子句不会影响后续的条件判断,每个条件都会单独进行判断。tgt_t3 中插入了 1 条数据,else 分支只会插入不满足前面所有条件的数据。

有条件的多表插入语句最多支持 127 个 when 子句。

有条件的 insert first 语句

 

有条件的 insert first 的原理和 case 表达式类似,只会执行第一个满足条件的插入语句,然后继续处理源数据中的其他记录。例如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

truncate table tgt_t1;

truncate table tgt_t2;

truncate table tgt_t3;

 

insert first

   when id <= 1 then

     into tgt_t1(id, name ) values (id, name )

   when id between 1 and 2 then

     into tgt_t2(id, name ) values (id, name )

   else

     into tgt_t3(id, name ) values (id, name )

select * from src_table;

 

select * from tgt_t1;

id| name   |

--|------|

  1|张三  |

 

select * from tgt_t2;

id| name   |

--|------|

  2|李四  |

 

select * from tgt_t3;

id| name   |

--|------|

  3|王五  |

以上语句和上一个示例的差别在于源数据中的每个记录只会插入一次,tgt_t2 中不会插入 id 等于 1 的数据。

多表插入语句的限制

 

oracle 多表插入语句存在以下限制:

多表插入只能针对表执行插入操作,不支持视图或者物化视图。 多表插入语句不能通过 db link 针对远程表执行插入操作。 多表插入语句不能通针对嵌套表执行插入操作。 所有 insert into 子句中的字段总数量不能超过 999 个。 多表插入语句中不能使用序列。多表插入语句被看作是单个语句,因此只会产生一个序列值并且用于所有的数据行,这样会导致数据问题。 多表插入语句不能和执行计划稳定性功能一起使用。 如果任何目标并使用了 parallel 提示,整个语句都会被并行化处理。如果没有目标表使用 parallel 提示,只有定义了 parallel 属性的目标表才会被并行化处理。 如果多表插入语句中的任何表是索引组织表,或者定义了位图索引,都不会进行并行化处理。

到此这篇关于oracle 数据仓库 etl 技术之多表插入语句的示例详解的文章就介绍到这了,更多相关oracle 多表插入内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/horses/article/details/115560296

查看更多关于Oracle 数据仓库ETL技术之多表插入语句的示例详解的详细内容...

  阅读:26次