Oracle Sql奇怪写法之- INSERT ALL & FIRST

[复制链接]
查看11 | 回复9 | 2012-5-15 22:04:44 | 显示全部楼层 |阅读模式
本帖最后由 南北思静 于 2015-1-16 23:49 编辑


一、INSERT ALL 不带条件用法

SQL> create table t_table1(tid number,tname varchar(100));

Table created

SQL> create table t_table2(tid number,tname varchar(100));

Table created

SQL> insert all
into t_table1(tid, tname)
values
(object_id, object_name)
into t_table2(tid, tname)
values
(object_id, object_name)
select object_id, object_name, object_type from dba_objectswhere wner = 'TEST';

8440 rows inserted

SQL> commit;

Commit complete

SQL> select count(1) from t_table1;

COUNT(1)
----------
4220

SQL> select count(1) from t_table2;

COUNT(1)
----------
4220

SQL>

指定所有跟随的多表,都执行无条件的多表插入;



二、INSERT ALL 带条件用法

SQL> create table t_table(tid number,tname varchar(100));

Table created

SQL> create table t_index(iid number,iname varchar(100));

Table created

SQL> create table t_other(oid number,oname varchar(100));

Table created

SQL> insert all when object_type = 'TABLE' then into t_table
2(tid, tname)
3values
4(object_id, object_name) when object_type = 'INDEX' then into t_index
5(iid, iname)
6values
7(object_id, object_name) else into t_other
8(oid, oname)
9values
10(object_id, object_name)
11select object_id, object_name, object_type
12from dba_objects
13 where wner = 'TEST';

4220 rows inserted

SQL> commit;

Commit complete

SQL> select count(1) from t_table;

COUNT(1)
----------
1025

SQL> select count(1) from t_index;

COUNT(1)
----------
1582

SQL> select count(1) from t_other;

COUNT(1)
----------
1613

SQL>

Oracle服务器通过相应的WHEN条件过滤,将查询结果分别插入到满足条件的表中;



三、INSERT FIRST 用法

SQL> create table t_table1(tid number,tname varchar(100));

Table created

SQL> create table t_table2(tid number,tname varchar(100));

Table created

SQL> create table t_table3(tid number,tname varchar(100));

Table created

SQL> insert first when object_idcommit;

Commit complete

SQL> select count(1) from t_table1;

COUNT(1)
----------
860

SQL> select count(1) from t_table2;

COUNT(1)
----------
2327

SQL> select count(1) from t_table3;

COUNT(1)
----------
1033

SQL>

可以看到,用FIRST后,凡是符合第一个条件的就都插入第一个表,其他的数据才在以后的条件里再判断。
=================================================
其实写到最后这让我想到了近期做预测项目中,遇到的匹配问题。(基于hive)
first很像在查询匹配中的like like只能找到符合条件的第一条记录
如果需要找到所有符合条件的并且提高效率,需要用正则或多模匹配算法。后绪有机会单说。

回复

使用道具 举报

千问 | 2012-5-15 22:04:44 | 显示全部楼层
干得漂亮!
回复

使用道具 举报

千问 | 2012-5-15 22:04:44 | 显示全部楼层
学习了....


回复

使用道具 举报

千问 | 2012-5-15 22:04:44 | 显示全部楼层
例子不错
回复

使用道具 举报

千问 | 2012-5-15 22:04:44 | 显示全部楼层
标题少了insert
回复

使用道具 举报

千问 | 2012-5-15 22:04:44 | 显示全部楼层
moseslin 发表于 2015-1-14 14:18
干得漂亮!

谢谢!互相学习!
回复

使用道具 举报

千问 | 2012-5-15 22:04:44 | 显示全部楼层
lz把字体放大些
回复

使用道具 举报

千问 | 2012-5-15 22:04:44 | 显示全部楼层
本帖最后由 芳林野草 于 2015-1-16 16:51 编辑
少见多怪。没事多翻翻Oracle官方手册,啥都明白了~
multi_table_insert
In a multitable insert, you insert computed rows derived from the rows returned from
the evaluation of a subquery into one or more tables.
Table aliases are not defined by the select list of the subquery. Therefore, they are not
visible in the clauses dependent on the select list. For example, this can happen when
trying to refer to an object column in an expression. To use an expression with a table
alias, you must put the expression into the select list with a column alias, and then
refer to the column alias in the VALUES clause or WHEN condition of the multitable
insert.
ALL into_clause
Specify ALL followed by multiple insert_into_clauses to perform an
unconditional multitable insert. Oracle Database executes each insert_into_clause
once for each row returned by the subquery.
conditional_insert_clause
Specify the conditional_insert_clause to perform a conditional multitable
insert. Oracle Database filters each insert_into_clause through the
corresponding WHEN condition, which determines whether that insert_into_clause is executed.
Each expression in the WHEN condition must refer to columns
returned by the select list of the subquery. A single multitable insert statement can
contain up to 127 WHEN clauses.
ALL If you specify ALL, the default value, then the database evaluates each WHEN
clause regardless of the results of the evaluation of any other WHEN clause. For each
WHEN clause whose condition evaluates to true, the database executes the
corresponding INTO clause list.
FIRST If you specify FIRST, then the database evaluates each WHEN clause in the
order in which it appears in the statement. For the first WHEN clause that evaluates to
true, the database executes the corresponding INTO clause and skips subsequent WHEN
clauses for the given row.
Note: from

回复

使用道具 举报

千问 | 2012-5-15 22:04:44 | 显示全部楼层
芳林野草 发表于 2015-1-16 16:51
少见多怪。没事多翻翻Oracle官方手册,啥都明白了~
multi_table_insert

还是您见识广~
回复

使用道具 举报

千问 | 2012-5-15 22:04:44 | 显示全部楼层
芳林野草 发表于 2015-1-16 16:51
少见多怪。没事多翻翻Oracle官方手册,啥都明白了~
multi_table_insert

只是个人之前的学习笔记整理和分享而已
不然除了编写Oracle官方手册的筒子们还没人敢谈oracle了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行