- 浏览: 1315327 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (471)
- 原创文章 (4)
- Database (84)
- J2SE (63)
- Web (26)
- Javascript (30)
- Lucene (11)
- os (13)
- 算法 (8)
- Webservice (1)
- Open projects (18)
- Hibernate (18)
- Spring (15)
- Css (2)
- J2ee (2)
- 综合技术 (18)
- 安全管理 (13)
- PatternsInJava (27)
- NIO (5)
- Ibatis (2)
- 书籍收藏 (1)
- quartz (7)
- 并发编程 (15)
- oracle问题 (2)
- ios (60)
- coco2d-iphone (3)
- C++ (6)
- Zookeeper (2)
- golang (4)
- animation (2)
- android (1)
最新评论
-
dandingge123:
【引用】限制UITextField输入长度的方法 -
qja:
...
对List顺序,逆序,随机排列实例代码 -
安静听歌:
现在在搞这个,,,,,哎~头都大了,,,又freemarker ...
通用大型网站页面静态化解决方案(一) -
springdata-jpa:
java quartz定时任务demo教程源代码下载,地址:h ...
Quartz 配置参考 -
马清天:
[b][/b][list][*]引用[u][/u][/list ...
通用大型网站页面静态化解决方案(一)
在Oracle 10g之前,merge语句支持匹配更新和不匹配插入2种简单的用法,在10g中Oracle对merge语句做了增强,增加了条件选项和DELETE操作。下面我通过一个demo来简单介绍一下10g中merge的增强和10g前merge的用法。
参考Oracle 的SQL Reference,大家可以看到Merge Statement的语法如下:
MERGE [hint] INTO [schema .] table [t_alias] USING [schema .]
{ table | view | subquery } [t_alias] ON ( condition )
WHEN MATCHED THEN merge_update_clause
WHEN NOT MATCHED THEN merge_insert_clause;
下面我在windows xp 下10.2.0.1版本上做一个测试看看
SQL> select * from v$version; BANNER ---------------------------------------------------------------- Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod PL/SQL Release 10.2.0.1.0 - Production CORE 10.2.0.1.0 Production TNS for 32-bit Windows: Version 10.2.0.1.0 - Production NLSRTL Version 10.2.0.1.0 - Production SQL> 一、创建测试用的表 SQL> create table subs(msid number(9), 2 ms_type char(1), 3 areacode number(3) 4 ); 表已创建。 SQL> create table acct(msid number(9), 2 bill_month number(6), 3 areacode number(3), 4 fee number(8,2) default 0.00); 表已创建。 SQL> SQL> insert into subs values(905310001,0,531); 已创建 1 行。 SQL> insert into subs values(905320001,1,532); 已创建 1 行。 SQL> insert into subs values(905330001,2,533); 已创建 1 行。 SQL> commit; 提交完成。 SQL> 二、下面先演示一下merge的基本功能 1) matched 和not matched clauses 同时使用 merge into acct a using subs b on (a.msid=b.msid) when MATCHED then update set a.areacode=b.areacode when NOT MATCHED then insert(msid,bill_month,areacode) values(b.msid,'200702',b.areacode); 2) 只有not matched clause,也就是只插入不更新 merge into acct a using subs b on (a.msid=b.msid) when NOT MATCHED then insert(msid,bill_month,areacode) values(b.msid,'200702',b.areacode); 3) 只有matched clause, 也就是只更新不插入 merge into acct a using subs b on (a.msid=b.msid) when MATCHED then update set a.areacode=b.areacode Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 Connected as study SQL> select * from subs; MSID MS_TYPE AREACODE ---------- ------- -------- 905310001 0 531 905320001 1 532 905330001 2 533 SQL> select * from acct; MSID BILL_MONTH AREACODE FEE ---------- ---------- -------- ---------- SQL> SQL> merge into acct a 2 using subs b on (a.msid=b.msid) 3 when MATCHED then 4 update set a.areacode=b.areacode 5 when NOT MATCHED then 6 insert(msid,bill_month,areacode) 7 values(b.msid,'200702',b.areacode); Done SQL> select * from acct; MSID BILL_MONTH AREACODE FEE ---------- ---------- -------- ---------- 905320001 200702 532 0.00 905330001 200702 533 0.00 905310001 200702 531 0.00 SQL> insert into subs values(905340001,3,534); 1 row inserted SQL> select * from subs; MSID MS_TYPE AREACODE ---------- ------- -------- 905340001 3 534 905310001 0 531 905320001 1 532 905330001 2 533 SQL> SQL> merge into acct a 2 using subs b on (a.msid=b.msid) 3 when NOT MATCHED then 4 insert(msid,bill_month,areacode) 5 values(b.msid,'200702',b.areacode); Done SQL> select * from acct; MSID BILL_MONTH AREACODE FEE ---------- ---------- -------- ---------- 905320001 200702 532 0.00 905330001 200702 533 0.00 905310001 200702 531 0.00 905340001 200702 534 0.00 SQL> update subs set areacode=999; 4 rows updated SQL> select * from subs; MSID MS_TYPE AREACODE ---------- ------- -------- 905340001 3 999 905310001 0 999 905320001 1 999 905330001 2 999 SQL> select * from acct; MSID BILL_MONTH AREACODE FEE ---------- ---------- -------- ---------- 905320001 200702 532 0.00 905330001 200702 533 0.00 905310001 200702 531 0.00 905340001 200702 534 0.00 SQL> SQL> merge into acct a 2 using subs b on (a.msid=b.msid) 3 when MATCHED then 4 update set a.areacode=b.areacode; Done SQL> select * from acct; MSID BILL_MONTH AREACODE FEE ---------- ---------- -------- ---------- 905320001 200702 999 0.00 905330001 200702 999 0.00 905310001 200702 999 0.00 905340001 200702 999 0.00 SQL> 三、10g中增强一:条件操作 1) matched 和not matched clauses 同时使用 merge into acct a using subs b on (a.msid=b.msid) when MATCHED then update set a.areacode=b.areacode where b.ms_type=0 when NOT MATCHED then insert(msid,bill_month,areacode) values(b.msid,'200702',b.areacode) where b.ms_type=0; 2) 只有not matched clause,也就是只插入不更新 merge into acct a using subs b on (a.msid=b.msid) when NOT MATCHED then insert(msid,bill_month,areacode) values(b.msid,'200702',b.areacode) where b.ms_type=0; 3) 只有matched clause, 也就是只更新不插入 merge into acct a using subs b on (a.msid=b.msid) when MATCHED then update set a.areacode=b.areacode where b.ms_type=0; Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 Connected as study SQL> select * from subs; MSID MS_TYPE AREACODE ---------- ------- -------- 905310001 0 531 905320001 1 532 905330001 2 533 SQL> select * from acct; MSID BILL_MONTH AREACODE FEE ---------- ---------- -------- ---------- SQL> SQL> merge into acct a 2 using subs b on (a.msid=b.msid) 3 when MATCHED then 4 update set a.areacode=b.areacode 5 where b.ms_type=0 6 when NOT MATCHED then 7 insert(msid,bill_month,areacode) 8 values(b.msid,'200702',b.areacode) 9 where b.ms_type=0; Done SQL> select * from acct; MSID BILL_MONTH AREACODE FEE ---------- ---------- -------- ---------- 905310001 200702 531 0.00 SQL> insert into subs values(905360001,0,536); 1 row inserted SQL> select * from subs; MSID MS_TYPE AREACODE ---------- ------- -------- 905360001 0 536 905310001 0 531 905320001 1 532 905330001 2 533 SQL> SQL> merge into acct a 2 using subs b on (a.msid=b.msid) 3 when NOT MATCHED then 4 insert(msid,bill_month,areacode) 5 values(b.msid,'200702',b.areacode) 6 where b.ms_type=0; Done SQL> select * from acct; MSID BILL_MONTH AREACODE FEE ---------- ---------- -------- ---------- 905310001 200702 531 0.00 905360001 200702 536 0.00 SQL> update subs set areacode=888 where ms_type=0; 2 rows updated SQL> select * from subs; MSID MS_TYPE AREACODE ---------- ------- -------- 905360001 0 888 905310001 0 888 905320001 1 532 905330001 2 533 SQL> select * from acct; MSID BILL_MONTH AREACODE FEE ---------- ---------- -------- ---------- 905310001 200702 531 0.00 905360001 200702 536 0.00 SQL> SQL> merge into acct a 2 using subs b on (a.msid=b.msid) 3 when MATCHED then 4 update set a.areacode=b.areacode 5 where b.ms_type=0; Done SQL> select * from acct; MSID BILL_MONTH AREACODE FEE ---------- ---------- -------- ---------- 905310001 200702 888 0.00 905360001 200702 888 0.00 SQL> 四、10g中增强二:删除操作 An optional DELETE WHERE clause can be used to clean up after a merge operation. Only those rows which match both the ON clause and the DELETE WHERE clause are deleted. merge into acct a using subs b on (a.msid=b.msid) when MATCHED then update set a.areacode=b.areacode delete where (b.ms_type!=0); SQL> select * from subs; MSID MS_TYPE AREACODE ---------- ------- -------- 905310001 0 531 905320001 1 532 905330001 2 533 SQL> select * from acct; MSID MS_TYPE AREACODE ---------- ------- -------- 905310001 0 531 905320001 1 532 905330001 2 533 SQL> SQL> merge into acct a 2 using subs b on (a.msid=b.msid) 3 when MATCHED then 4 update set a.areacode=b.areacode 5 delete where (b.ms_type!=0); Done SQL> select * from acct; MSID MS_TYPE AREACODE ---------- ------- -------- 905310001 0 531 SQL> 更为详尽的语法,请参考Oracle SQL Reference手册! 原文链接:http://tomszrp.itpub.net/post/11835/263865
存储过程的实现新增去重复
tzxd@webora9> drop table t ; Table dropped. tzxd@webora9> create table t (t1 number(5),t2 varchar2(20)); Table created. tzxd@webora9> create index idx_t_t1 on t(t1); Index created. tzxd@webora9> declare 2 v_dummy number(1); 3 begin 4 select count(*) into v_dummy from t where t1 = 1 for update; 5 if v_dummy = 1 then 6 dbms_output.put_line('value 1 of t1 is exists '); 7 else 8 insert into t values(1,'test'); 9 end if; 10 end; 11 / PL/SQL procedure successfully completed. tzxd@webora9> ed Wrote file aaa.sql declare v_dummy number(1); begin select count(*) into v_dummy from t where t1 = 1 for update; if v_dummy = 1 then dbms_output.put_line('value 1 of t1 is exists '); else insert into t values(1,'test'); end if; end; / 1 declare 2 v_dummy number(1); 3 begin 4 select count(*) into v_dummy from t where t1 = 1 for update; 5 if v_dummy = 1 then 6 dbms_output.put_line('value 1 of t1 is exists '); 7 else 8 insert into t values(1,'test'); 9 end if; 10* end; tzxd@webora9> / PL/SQL procedure successfully completed. tzxd@webora9> select * from t; T1 T2 ---------- -------------------- 1 test
发表评论
-
mysql主从热备配置(含innodb)终极版
2012-12-25 13:10 2591转自 http://blogread.cn/it/articl ... -
sqlite3中的数据类型
2012-12-10 21:37 1268(转)http://www.cnblogs.com/kfqco ... -
Mac下MySql卸载方法
2012-09-10 23:57 1019Mac下MySql卸载方法 mac下mysql的D ... -
mac os x mysql数据库utf8配置
2012-09-10 23:29 2230进入mysql console: 输入 mysql& ... -
ON DUPLICATE KEY UPDATE
2012-08-07 01:47 1052(转自)http://blog.csdn.net/kesaih ... -
MySQL单列索引和组合索引的区别介绍
2012-08-07 01:31 1068(转自)http://blog.cs ... -
centos下MySQL主从同步配置
2012-08-03 13:14 1521(转自)http://apps.hi.baidu.com ... -
sql语句中left join、inner join中的on与where的区别
2012-06-13 13:24 1227table a(id, type): id t ... -
分组取前N记录
2012-05-31 16:24 1093(转)http://blog.csdn.net/ac ... -
如何一条sql语句取出分组数据中值最大的
2012-05-30 23:59 2322--按某一字段分组取最大(小)值所在行的数据(2007-10- ... -
存储过程与函数的区别
2011-09-28 19:35 1127... -
RMAN备份方案
2011-07-21 10:29 2027RMAN备份方案 RMAN也可以进行联机备份,而且备份与恢 ... -
oracle
2011-04-06 17:46 1019学习笔记 -
PLSQL Developer + Oracle客户端简易安装方法
2011-03-17 00:10 14332Oracle 10g绿色客户端 pl ... -
SELECT INTO FROM与INSERT INTO SELECT
2011-01-26 15:52 33751.INSERT INTO SELECT语句 ... -
浅谈unique列上插入重复值的MySQL解决方案
2011-01-25 11:33 1753本文的unique列上插入重复值解决方案,主要基于MySQL平 ... -
单行函数(仅对单个表达式或一行进行处理)
2011-01-23 18:49 1235例如:select sum(qty) from sales ... -
oracle中的事务控制命令
2011-01-23 18:45 2017事务提交有显示提交:使用commit命令来提交所有未提交的更新 ... -
oracle全文检索
2011-01-23 18:18 61111.全文检索和普通检索的区别 不使用Oracle text功 ... -
Oracle快照原理及实现总结
2011-01-23 16:41 2436Oracle数据库的快照是一个表,它包含有对一个本地或远程数据 ...
相关推荐
根据一个源数据表对另一个数据表进行确定性的插入、更新和删除这样复杂的操作,运用新的MERGE语句,开发者用一条命令就可以完成。对两个表进行信息同步时,有三步操作要进行。首先要处理任何需要插入目标数据表的新...
Oracle中merge into的使用 很有用的哦 学习一下
浅谈Oracle数据库中Merge Into的用法.pdf
关于Oracle中merge into的用法,比较实用哦
主要介绍了在MySQL中实现插入或更新操作(类似Oracle的merge语句)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
详细介绍了使用 BULK COLLECT 进行批量操作 提高sql的执行效率 使用MERGE INTO USING 一条sql搞定 新增和修改 使用connect by 进行递归树查询
oracle merge 用法详解
在Oracle 9i R2版中引入的MERGE语句通常被称作“更新插入”(upsert),因为使用MERGE可以在同一个步骤中更新(update)并插入(insert)数据行。。。。。。
oracle同一个表中使用merge
MERGE是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。通过MERGE语句,根据一张表(原数据表,source table)或子查询的连接条件对另外一张(目标表,target table)表进行查询,连接条件匹配上的进行UPDATE,...
通过这个MERGE你能够在一个SQL语句中对一个表同时执行INSERT和UPDATE操作. 本博客介绍一下Oracle merge合并函数,业务场景:新增数据的时候要先查询数据库是否已经有改数据,有数据就更新数据,没数据才新增数据,这...
oracle Merge 函数
oracle merge into的使用,开发必备的。
DB2 merge语句使用教程
实际项目中遇到的问题总结:数据量百万级,千万级。Oracle中update语句的优化,一共四种方案,工作中遇到该类问题可以参考。
DB2中的Merge语句可以将一个表中的数据合并到另一个表中,在合并的同时可以根据条件进行插入、删除、更新等操作,功能非常强大。
Bug 和对新功能的建议应当提交到 sourceforge.net 的 bug 和 RFE trackers 上。 Bug tracker: http://sourceforge.net/tracker/?group_id=13216&atid=113216 当您提交 bug 时,请告诉我们 WinMerge 的版本号! Win...