[20150115]insert多个表.txt

简介: [20150115]insert多个表.txt --别人给我提出一个问题,要把表拆开2个表,能否快速完成这个工作。还是通过例子来说明: SCOTT@test> @ver1 PORT_STRING                    VERSION  ...

[20150115]insert多个表.txt

--别人给我提出一个问题,要把表拆开2个表,能否快速完成这个工作。还是通过例子来说明:

SCOTT@test> @ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.3.0     Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

create table t1 (id number,name varchar2(20),c1 varchar2(20),c2 varchar2(20),c3 varchar2(20),c4 varchar2(20));

insert into t1 values (1,'a','a1','a2','a3','a4');
insert into t1 values (2,'b','b1','b2',NULL,NULL);
insert into t1 values (3,'c','c1','c2','c3',NULL);

--要把表变成两个:
--表t1x:
1,'a'
2,'b'
3,'c'

--表t2x
1,1,'a1','a2'
1,2,'a3','a4'
2,1,'b1','b2'
3,1,'c1','c2'

--有错误的记录在另外的表中:
--t2err
3,2,'c3',NULL

--实际上真正生产系统要折返的字段更多,这个明显违反关系数据结构的设计,国内的开发团队那个时候能提高,在数据结构上出现错误
--,要修改代码的工程量很大的。

--自己开始考虑多个select在插入,明显效率不高。有考虑11g的pivot,可以参考链接:
--http://www.oracle-base.com/articles/11g/pivot-and-unpivot-operators-11gr1.php

--昨天看了sql 的参考手册,才想起来使用insert多个表的操作最简单。操作如下:


1.建立表:

create table t1x   (id number,name varchar2(20));
create table t2x   (id number,seq number,c1 varchar2(20),c2 varchar2(20));
create table t2err (id number,seq number,c1 varchar2(20),c2 varchar2(20));


2.执行如下:

insert all
   when 1 =1 then
       into t1x values (id,name)
   when c1 is not null and c2 is not null then
       into t2x values (id,1,c1,c2)
   when c3 is not null and c4 is not null then
       into t2x values (id,2,c3,c4)
   when ( c1 is null and c2 is not null ) or ( c1 is not null and c2 is null ) then
       into t2err values (id,1,c1,c2)
   when ( c3 is null and c4 is not null ) or ( c3 is not null and c4 is null ) then
       into t2err values (id,2,c3,c4)
   select * from t1;

SCOTT@test> @dpc ''  ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  0yy08jpx5vcqk, child number 0
-------------------------------------
insert all    when 1 =1 then        into t1x values (id,name)    when
c1 is not null and c2 is not null then        into t2x values
(id,1,c1,c2)    when c3 is not null and c4 is not null then        into
t2x values (id,2,c3,c4)    when ( c1 is null and c2 is not null ) or (
c1 is not null and c2 is null ) then        into t2err values
(id,1,c1,c2)    when ( c3 is null and c4 is not null ) or ( c3 is not
null and c4 is null ) then        into t2err values (id,2,c3,c4)
select * from t1

Plan hash value: 1248537433

----------------------------------------------------------------------------------------------------
| Id  | Operation           | Name  | Starts | E-Rows | Cost (%CPU)| A-Rows |   A-Time   | Buffers |
----------------------------------------------------------------------------------------------------
|   0 | INSERT STATEMENT    |       |      1 |        |     3 (100)|      0 |00:00:00.01 |      24 |
|   1 |  MULTI-TABLE INSERT |       |      1 |        |            |      0 |00:00:00.01 |      24 |
|   2 |   TABLE ACCESS FULL | T1    |      1 |      3 |     3   (0)|      3 |00:00:00.01 |       7 |
|   3 |   INTO              | T1X   |      0 |        |            |      0 |00:00:00.01 |       0 |
|   4 |   INTO              | T2X   |      0 |        |            |      0 |00:00:00.01 |       0 |
|   5 |   INTO              | T2X   |      0 |        |            |      0 |00:00:00.01 |       0 |
|   6 |   INTO              | T2ERR |      0 |        |            |      0 |00:00:00.01 |       0 |
|   7 |   INTO              | T2ERR |      0 |        |            |      0 |00:00:00.01 |       0 |
----------------------------------------------------------------------------------------------------


SCOTT@test> select * from t1x;
        ID NAME
---------- --------------------
         1 a
         2 b
         3 c

SCOTT@test> select * from t2x;
        ID        SEQ C1                   C2
---------- ---------- -------------------- --------------------
         1          1 a1                   a2
         2          1 b1                   b2
         3          1 c1                   c2
         1          2 a3                   a4

SCOTT@test> select * from t2err;
        ID        SEQ C1                   C2
---------- ---------- -------------------- --------------------
         3          2 c3

--注意这里使用了all,要求记录走遍全部条件分支,满足就插入。使用first表示满足1个条件就插入,剩下的不再判断。
--btw :以前也写过一篇http://blog.itpub.net/267265/viewspace-713115/,这种技巧不常用,有点忘记了。
--实际上还可以选择有条件的插入,特此做一个记录。

目录
相关文章
|
关系型数据库 MySQL 索引
浅谈create table as 和 insert into select 复制表遇到的问题
之前做一次表压缩测试,在准备原表时需要数据量比较大的表,通过insert into select 的方式将几个表的数据复制到一个表,产生的一些问题~
3134 0
|
SQL Oracle 关系型数据库
[20170724]关于sql_id那些事.txt
[20170724]关于sql_id那些事.txt --//昨天别人问的问题,我以前也写过许多blog,做一些总结: http://blog.itpub.net/267265/viewspace-1357292/ http://blog.
1073 0
[20170513]update结果集.txt
[20170513]update结果集.txt --//前一阵子要对收费价格进行大量调整,当时开发随手写的sql非常不好,我建议通过结果集来修改,这样简单不容易出错. --//通过例子说明: 1.
818 0
|
SQL Shell 测试技术
[20161023]为什么以前可以这样的表.txt
[20161023]为什么以前可以这样的表.txt --上午看https://oracleblog.org/working-case/ora-01401-impdp-same-character/ CREATE TABLE ASS_ACC...
774 0
|
Oracle 关系型数据库 OLAP
[20160910]快速修改表的schema.txt
[20160910]快速修改表的schema.txt --以前也做过例子: http://blog.itpub.net/267265/viewspace-741154/ http://blog.itpub.net/267265/viewspace-744787/ --第1种就是修改数据字典的情况,但是这种存在一定的风险,我当时的测试版本11.2.0.1还有修改obj$的字段spare3. --第2种就是利用交换分区的方法。
828 0
CTAS和insert append的一个测试
该文章写于2004年,最近刚从以前的blog上迁移出来。 [@more@]8174上的一个测试,非归档模式: 代码: SQL> select * from v$version; BANNER -----------------------------------...
978 0
|
SQL 关系型数据库
[20150616]关于sql_id.txt
[20150616]关于sql_id.txt --我曾经提到PLSQL中使用绑定变量,oracle会格式化处理,转化为特定的格式。可以参考我以前的例子: --[20121102]PLSQL中的绑定变量.txt --http://blog.itpub.net/267265/viewspace-748190/ --我也曾经写过一篇exact_matching_signature,force_matching。
810 0
[20141205]关于sql_id.txt
[20141205]关于sql_id.txt --昨天跟别人聊天,讲sql_id字串里面没有字符'o'.实际上他在学习sql_id与hash_value转换时copy 和 paste少粘贴1位,手工输入时以 --为是--字母'o',实际上是数字'0'.
697 0
|
测试技术
[20141116]12c下增加字段与缺省值.txt
[20141116]12c下增加字段与缺省值.txt --前一段时间写了一篇表增加字段与缺省值的blog. --链接如下: http://blog.itpub.net/267265/viewspace-1257035/ --12G 增加字段带缺省值,可以很快完成,不需要update表.实际上是增加一个隐含字段,通过位与的方式确定取值方式。
813 0