oracle的多表插入
来源:互联网 发布:淘宝品牌信息怎么写 编辑:程序博客网 时间:2024/06/04 17:44
oracle的多表插入操作。在业务处理过程中,经常会碰到将业务数据按照条件分别插入不同的数据表的问题,按照传统的处理方式,需要分条件执行多次检索后分别插入不同的表单,这样因为执行了重复的检索造成cpu和内存的浪费,从oracle9i开始引入了insert all关键字支持将某张表的数据同时插入多张表单。语法如下:
Insert all Insert_into_clause [value_clause] subquery;
Insert conditional_insert_clause subquery;
如上所示,insert_into_clause用于指定insert子句;value clause用于指定值子句;subquery用于指定提供数据的子查询;condition_insert_clause用于指定insert条件子句。
当使用all操作符执行多表插入时,在每个条件子句上都要执行into子句后的子查询,并且条件中使用的列必须在插入和子查询的结果集中:
--创建测试用表
createtable tdate(
idvarchar2(10),
namevarchar2(20),
birthday datedefaultsysdate
);
--插入数据
insertinto tdate values(1,'zhangsan',to_date('1980-05-10','YYYY-MM-DD'));
insertinto tdate values(1,'zhangsan',to_date('1980-05-10','YYYY-MM-DD'));
insertinto tdate values(2,'lisi',to_date('1980-05-10','YYYY-MM-DD'));
insertinto tdate values(3,'wangwu',default);
insertinto tdate(id,name) values(4,'zhangsan');
commit;
--创建接收用测试表
createtable tdate1 asselect * from tdate where1=0;
createtable tdate2 asselect * from tdate where1=0;
commit;
--使用all关键字执行多表插入操作
insertall
when birthday > '01-1月-08'theninto tdate1
when birthday < '01-1月-08'theninto tdate2
whenname = 'zhangsan'theninto tdate1
whenname = 'lisi'theninto tdate2
select * from tdate;
在上述操作语句中,如果原表tdate中存在既满足birthday > '01-1月-08'又满足name = 'zhangsan'的数据,那么将执行两次插入。而使用first关键字就可以避免这个问题。使用first关键字时,如果有记录已经满足先前条件,并且已经被插入到某个表单中(未必非要是同一个表),那么该行数据在后续插入中将不会被再次使用。也就是说使用first关键字,原表每行数据按照执行顺序只会被插入一次。
insertfirst
when birthday > '01-1月-08'theninto tdate1
when birthday < '01-1月-08'theninto tdate2
whenname = 'zhangsan'theninto tdate1
whenname = 'lisi'theninto tdate2
select * from tdate;
Insert all Insert_into_clause [value_clause] subquery;
Insert conditional_insert_clause subquery;
如上所示,insert_into_clause用于指定insert子句;value clause用于指定值子句;subquery用于指定提供数据的子查询;condition_insert_clause用于指定insert条件子句。
当使用all操作符执行多表插入时,在每个条件子句上都要执行into子句后的子查询,并且条件中使用的列必须在插入和子查询的结果集中:
--创建测试用表
createtable tdate(
idvarchar2(10),
namevarchar2(20),
birthday datedefaultsysdate
);
--插入数据
insertinto tdate values(1,'zhangsan',to_date('1980-05-10','YYYY-MM-DD'));
insertinto tdate values(1,'zhangsan',to_date('1980-05-10','YYYY-MM-DD'));
insertinto tdate values(2,'lisi',to_date('1980-05-10','YYYY-MM-DD'));
insertinto tdate values(3,'wangwu',default);
insertinto tdate(id,name) values(4,'zhangsan');
commit;
--创建接收用测试表
createtable tdate1 asselect * from tdate where1=0;
createtable tdate2 asselect * from tdate where1=0;
commit;
--使用all关键字执行多表插入操作
insertall
when birthday > '01-1月-08'theninto tdate1
when birthday < '01-1月-08'theninto tdate2
whenname = 'zhangsan'theninto tdate1
whenname = 'lisi'theninto tdate2
select * from tdate;
在上述操作语句中,如果原表tdate中存在既满足birthday > '01-1月-08'又满足name = 'zhangsan'的数据,那么将执行两次插入。而使用first关键字就可以避免这个问题。使用first关键字时,如果有记录已经满足先前条件,并且已经被插入到某个表单中(未必非要是同一个表),那么该行数据在后续插入中将不会被再次使用。也就是说使用first关键字,原表每行数据按照执行顺序只会被插入一次。
insertfirst
when birthday > '01-1月-08'theninto tdate1
when birthday < '01-1月-08'theninto tdate2
whenname = 'zhangsan'theninto tdate1
whenname = 'lisi'theninto tdate2
select * from tdate;
- oracle的多表插入
- oracle的多表插入
- Oracle的多表插入
- 简单的多表插入(oracle)
- oracle多表插入
- Oracle多表插入
- oracle 多表插入
- oracle 多表插入
- oracle多表插入
- Oracle多表插入
- oracle多表插入
- oracle select表插入,多行插入
- oracle的多行插入
- oracle的多表关联更新或者插入
- Oracle多表插入insert all/insert first的区别
- Oracle 插入多条记录的sql
- oracle插入多条语句(转载的)
- Oracle插入表中不存在的记录
- 作为Web开发人员,我为什么喜欢Google Chrome浏览器
- Ubuntu Linux系统下Gnome桌面快捷配置
- joj1171
- android开发视频教材笔记:第一季 9--13课
- 二十五条哈佛励志金句
- oracle的多表插入
- joj1082
- 说书(评书)术语
- 100%解决“该内存不能为read或written”的方法
- How Browsers Work: Behind the Scenes of Modern Web Browsers
- android开发视频教材笔记:第一季 14--19课
- 做程序员喜欢的测试----(网上摘录,以备后用)
- time.schedule方法参数解释
- Oracle 数组的学习