昨天crazylaa兄弟,给了个我个MERGE INTO的例子,你有福了引用 9 楼 java3344520 的回复:8.选择由你(针对同一数据表是选择插入,还是更新) merge into a using b on (a.USER_NO=b.USER_NO) when matched then update set a.NAME=b.NAME when not matched then insert values (b.USER_NO,b.NAME) 如何使用?有例子么 有: --Oracle merge into 的用法详解实例 -- 作用:merge into 解决用B表跟新A表数据,如果A表中没有,则把B表的数据插入A表; -- 语法: -- MERGE INTO [your table-name] [rename your table here] -- USING ( [write your query here] )[rename your query-sql and using just like a table] -- ON ([conditional expression here] AND [...]...) -- WHEN MATHED THEN [here you can execute some update sql or something else ] -- WHEN NOT MATHED THEN [execute something else here ! ] -------实例------- drop table a; drop table b; create table a(id number(1),name varchar2(10)); create table b(id number(1),name varchar2(10)); insert into a values(1,'2'); insert into b values(1,'1'); insert into b values(2,'2'); commit; merge into a using (select id,name from b ) c on(a.id=c.id ) when not matched then insert (a.id,a.name) values (c.id,c.name); merge into a using (select id,name from b ) c on(a.id=c.id ) when matched then update set a.name=c.name; merge into a using (select id,name from b ) c on(a.id=c.id ) when matched then update set a.name=c.name when not matched then insert (a.id,a.name) values (c.id,c.name); 作用:将表 b 数据 更新到 表a ,条件是a.id=b.id,如果a表中有该条件的数据就修改,没有该条件的数据就插入。 如果你的数据量很大,此sql效率非常高。
--Oracle merge into 的用法详解实例 -- 作用:merge into 解决用B表跟新A表数据,如果A表中没有,则把B表的数据插入A表; -- 语法: -- MERGE INTO [your table-name] [rename your table here] -- USING ( [write your query here] )[rename your query-sql and using just like a table] -- ON ([conditional expression here] AND [...]...) -- WHEN MATHED THEN [here you can execute some update sql or something else ] -- WHEN NOT MATHED THEN [execute something else here ! ] -------实例------- drop table a;
drop table b;
create table a(id number(1),name varchar2(10));
create table b(id number(1),name varchar2(10));
insert into a values(1,'2');
insert into b values(1,'1');
insert into b values(2,'2');
commit; merge into a
using (select id,name from b ) c
on(a.id=c.id )
when not matched then insert (a.id,a.name) values (c.id,c.name);
merge into a
using (select id,name from b ) c
on(a.id=c.id )
when matched then update set a.name=c.name; merge into a
using (select id,name from b ) c
on(a.id=c.id )
when matched then update set a.name=c.name
when not matched then insert (a.id,a.name) values (c.id,c.name);
作用:将表 b 数据 更新到 表a ,条件是a.id=b.id,如果a表中有该条件的数据就修改,没有该条件的数据就插入。 如果你的数据量很大,此sql效率非常高。
初次接触这个,望多指教
table2上俩事务,ID=1000的提交了,
在你JOB开始时,ID=998的刚好未提交。于是乎,你从1001取起,下次的开始ID更大,那么,ID=998的不是没取过去?
TABLE2的ID自动增长,
TABLE1的ID需要自动增长?TABLE1他的ID不是从TABLE2复制来的?
--这样不就可以了
insert into table1 values(table1seq.nextVal,table2.col2,table2.col3);
table1 中的ID是他自己的sequence,因为在查询 table2的数据时,还用了group by操作,所以 table2 的ID不能复制过来,
id 省 市 时间
1 江苏 常州 2010-02-10-15:33:30
2 江苏 常州 2010-02-10-15:34:30
3 江苏 常州 2010-02-10-16:33:30
4 江苏 常州 2010-02-10-16:34:30
5 江苏 扬州 2010-02-10-15:33:30
6 江苏 扬州 2010-02-10-15:34:30那么 table1中数据应该是
ID 省 市 数量 时间段
1 江苏 常州 2 2010021015
2 江苏 常州 2 2010021016
3 江苏 扬州 2 2010021015