[Oracle] Insert All的妙用

无条件的插入

Oracle中的insert all是指把同一批数据插入到不同的表中,假如现在有个需求:把t表中的数据分别插入t1,t2,如果你不知道insert all,你可能会使用insert插入2次,如下所示:

insert  into t1(object_name,object_id) select * from t;
insert  into t2(object_name,object_id) select * from t;
commit;

事实上,以上这种写法是错误的,因为在两次insert的过程中,t表的数据有可能已经发生了变化,也就是说,t1,t2表得到的数据有可能不一样,正确的写法应该是采用insert all:

insert all
into t1(object_name,object_id)
into t2(object_name,object_id)
select * from t;
commit;

有条件的插入

insert first/all 是对每一行来进行判断

两者区别:

insert first:对于每一行数据,只插入到第一个when条件成立的表,不继续检查其他条件。

insert all : 对于每一行数据,对每一个when条件都进行检查,如果满足条件就执行插入操作。

看下面的例子:

--insert first
--前面等于1的条件被<=5含在内,FIRST就表示前面插入了,后面不会再插入了。
insert first
when object_id = 1 then
into t1(object_name,object_id)
when object_id <=5 then
into t2(object_name,object_id)
select * from t;
commit;

select * from t1;

OBJECT_NAME                OBJECT_ID
--------------------------------- ---
ICOL$                              1

select * from t2;

OBJECT_NAME                OBJECT_ID
--------------------------------- ---
I_USER1                            2
CON$                               3
UNDO$                              4
C_COBJ#                            5

--insert all
insert all
when object_id = 1 then
into t1(object_name,object_id)
when object_id <=5 then
into t2(object_name,object_id)
select * from t;
commit;

SQL> select * from t1;

OBJECT_NAME                OBJECT_ID
--------------------------------- ---
ICOL$                              1
SQL> select * from t2;

OBJECT_NAME                OBJECT_ID
--------------------------------- ---
ICOL$                              1
I_USER1                            2
CON$                               3
UNDO$                              4
C_COBJ#                            5

行转列插入

insert all还可以实现行转列插入:

select * from sales_source_data;
EMPLOYEE_ID    WEEK_ID  SALES_MON  SALES_TUE  SALES_WED SALES_THUR  SALES_FRI
----------- ---------- ---------- ---------- ---------- ---------- ----------
        176          6       2000       3000       4000       5000       6000

insert all
into sales_info values(employee_id,week_id,sales_mon)
into sales_info values(employee_id,week_id,sales_tue)
into sales_info values(employee_id,week_id,sales_wed)
into sales_info values(employee_id,week_id,sales_thur)
into sales_info values(employee_id,week_id,sales_fri)
select employee_id,week_id,sales_mon,sales_tue,
sales_wed,sales_thur,sales_fri
from sales_source_data;

select * from sales_info;
EMPLOYEE_ID       WEEK      SALES
----------- ---------- ----------
        176          6       2000
        176          6       3000
        176          6       4000
        176          6       5000
        176          6       6000

[Oracle] Insert All的妙用

时间: 2024-10-12 03:55:40

[Oracle] Insert All的妙用的相关文章

Oracle Case When的妙用

Case when 的用法 --简单Case函数 简单CASE表达式,使用表达式确定返回值. 语法: CASE search_expression WHEN expression1 THEN result1 WHEN expression2 THEN result2 ... WHEN expressionN THEN resultN ELSE default_result --搜索CASE表达式 使用条件确定返回值. 语法: CASE WHEN condition1 THEN result1 W

oracle[insert 时报错: 单行子查询返回多行]

-- 错误的写法 insert into t_b_partner_vehicle(id, partner_id, vehicle_id) (seq_t_b_partner_vehicle.nextval,121, (select id from t_b_car_info where org_id in(1441,1427))) -- Error 单行子查询返回多行 -- 正确的写法 insert into t_b_partner_vehicle(id, partner_id, vehicle_i

Oracle insert all语句介绍

Oracle 中insert语句的高级用法,INSERT ALL 语句介绍: 1.无条件insert all 全部插入 CREATE TABLE t1(product_id NUMBER, product_name VARCHAR2(80),MONTH NUMBER); INSERT INTO t1 VALUES(111, '苹果',1); INSERT INTO t1 VALUES(222, '橘子',1); INSERT INTO t1 VALUES(333, '香蕉',1); COMMIT

oracle Insert 一次插入多条记录

oracle Insert 一次插入多条记录有两种方法: 1)Insert All Into table_name values ... insert all into table_name values('111','122') into table_name values('222','222') select 1 from dual; 2)Insert Into table_name select from insert into table1_name select t1.column_

oracle insert用法总结

总结下Oracle 中的Insert用法 1.标准Insert --单表单行插入 语法: INSERT INTO table [(column1,column2,...)] VALUE (value1,value2,...) 例子: insert into dep (dep_id,dep_name) values(1,'技术部'); 备注:使用标准语法只能插入一条数据,且只能在一张表中插入数据 2, 无条件 Insert all --多表多行插入 语法: INSERT [ALL] [condit

Oracle Insert BLOB

How to Insert Blobdata(image, video) intooracle BLOB size In this post it is shown how I can insert Blob data link image video into oracle database and also how I can determine the size of the BLOB data from oracle. 1)Create Directory Where BLOB resi

oracle insert &amp;字符插入问题

例如执行一下语句: insert into NSRXT_SP (SP_ID, SP_TITLE, SP_DESC, SP_URL, SP_TYPE, SP_SUB_TYPE, ADD_TIME, CZRY_DM) values (227, '[提醒]新所得税申报表填写小提示', null, 'http://hdsw.wangzhanbaomu.com/index.php?g=Wap&m=Index&a=content&id=561&classid=79&token=

Oracle Insert 多行(转)

1.一般的insert 操作. 使用语法insert into table_name[(column[,column...])] values (value[,value…])的insert语句,每条insert只能插入到目标表中一条指定的数据.如果有很多行需要插入,而且这些数据来源于别 的表或多个表之间提取的数据,那么这就不能满足要求了.因此oracle提供了一个扩展,见下面描述. 2.insert into table_name[(column[,column…])] select_stat

Oracle insert hang引发的行锁等待

问题描述: 客户方前台应用某个操作hang住,无报错也未有操作正常结束提示. 查看锁信息如下: SQL> with lk as (select blocking_instance||'.'||blocking_session blocker, inst_id||'.'||sid waiter 2              from gv$session 3              where blocking_instance is not null 4                and