阿拉丁的神灯
===========================================================
oracle merge into 的用法
===========================================================
所有的MIS系统都存在一个同样的需求,就是对于特定的数据,在一次批量操作过程中,如果数据已经存在,则对存在的数据按照现有情况进行更新,如果不存在,则需要加入数据库。这时,我们就可以考虑采用 Oracle 的 MERGE 函数,其具体用法如下:
-------------------------------------------------------------------------------- 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 ! ] --------------------------------------------------------------------------------   下面我再进行详细的说明:   上述代码格式中的加粗字体表示为 Oracle 关键字,[]以及其中的文字均是说明,在实际使用中不应有 [ words ] 出现。要注意()[圆括号]也是程序的组成部分。   为了能够使问题与实际问题更加贴切,不妨假设我们现在要给计算机系某个班的学生批量录入学生成绩。但是,录入时,如果学生的成绩已经存在时,老师只想对成绩进行修改,而如果成绩不存在则直接添加到库中。我们就老师的这些需求来构造一个执行语句。 -------------------------------------------------------------------------------- DEFINE TABLE : SCORE : using for save the students' score informations STUDENTS : the base information of students DEFINE COLUMNS : STUNO : the students' ID in the University STUNAME : students' name COURSENAME : course name COURSESCORE : the study-results of the reference course CLASSNAME : where the students study in STUGRADE : the students grade TERMNAME : the term which the reference course studied NOW BEAGIN TO WRITE DOWN THE STATEMENT HERE BLOW THIS LINE ! MERGE INTO SCORE S USING ( SELECT A.*,B.*,? MYSCORE FROM SCORE A,STUDENT B WHERE A.CLASSNO=? AND A.GRADE=? AND A.TERMNAME=? AND A.COURSENAME=? A.STUNO=B.STUNO(+) )X ON (S.STUNO=X.STUNO) WHEN MATHED THEN UPDATE SET COURSESCORE=X.MYSCORE WHEN NOT MATHED THEN INSERT ( STUNO,STUNAME,COURSENAME,COURSESCORE, CLASSNAME,STUGRADE,TERMNAME ) VALUES ( X.STUNO,X.STUNAME,X.COURSENAME,X.MYSCORE, X.CLASSNAME,X.STUGRADE,X.TERMNAME ); --------------------------------------------------------------------------------   注意到 MERGE 语句在最后的“;”(分号),这仅仅带到 MERGE 为一条完整的 SQL 语句。   这时,如果你需要在你的 Java 程序中使用上述方法执行相应操作,则仅需要将其放入一个 for 循环中即可。由于是批量更新数据,因此,如果你不想对中间出现异常的数据进行提交,导致数据的不完整,则可以考虑使用 Java 的事务回滚机制。具体示例代码如下: -------------------------------------------------------------------------------- public yourMethod(statement,...){ try{ Connection conn=...; PreparedStatement ps=...; Resultset rs=...; conn.setAutoCommit(false); for(int i=0;i
zzw0598 发表于:2006.08.20 21:24 ::分类: ( oracle开发 ) ::阅读:(131373次) :: 评论 (25)
re: oracle merge into 的用法 [回复]

laughingtonguesad.gifwink.giflaughingsmilesmile

alating 评论于: 2006.08.21 10:39
re: oracle merge into 的用法 [回复]

没有格式好难看

曾 评论于: 2006.08.26 10:28
re: oracle merge into 的用法 [回复]

没有格式好难看

曾 评论于: 2006.08.26 10:29
re: oracle merge into 的用法 [回复]

MIS系统除了考虑简便外,很大一个问题是它要不断改动升级,可能运行的环境会有变化。在写MIS类程序对应的SQL语句的时候,我总是不敢写过于特别的语句,因为我们程序都是同时支持多种DBMS的,害怕引起不兼容性而导致系统可维护性不好。有的时候真郁闷,SQL语言当初诞生就是为了解决各种DBMS的不一致问题,可是现在大型的DBMS之间符合标准SQL的功能就那么几个,也不说快点把SQL再提高提高。

香乡 评论于: 2006.09.01 18:44
疑问 [回复]

想问大家点问题,北京到上海的机票最低多少钱可以买到?还有这几个英文词是什么意思:wow gold,world of warcraft gold,blackjack,知道的加我QQ555553233

hsefmil 评论于: 2006.09.29 00:36
re: oracle merge into 的用法 [回复]

this

cheap@cheap.com 评论于: 2006.09.29 14:57
re: oracle merge into 的用法 [回复]

this

cheap@cheap.com 评论于: 2006.10.12 08:47
re: oracle merge into 的用法 [回复]

this

cheap@cheap.com 评论于: 2006.10.12 08:47
re: oracle merge into 的用法 [回复]

this

cheap@cheap.com 评论于: 2006.10.12 08:49
re: oracle merge into 的用法 [回复]

this

cheap@cheap.com 评论于: 2006.10.12 08:49
re: oracle merge into 的用法 [回复]

Hello, very nice site! Please also visit my homepages:

Thanks!

Barbara 评论于: 2006.10.21 18:08
re: oracle merge into 的用法 [回复]

Hello, very nice site! Please also visit my homepages:

Thanks!

Wolf 评论于: 2006.10.21 22:51
re: oracle merge into 的用法 [回复]

Hello, very nice site! Please also visit my homepages:

Thanks!

Gangster 评论于: 2006.10.22 03:32
re: oracle merge into 的用法 [回复]

this

cheap@cheap.com 评论于: 2006.11.22 20:14
re: oracle merge into 的用法 [回复]

博主,你该清理垃圾回复了crying.gif

小生 评论于: 2006.11.27 16:00
re: oracle merge into 的用法 [回复]

Very good zone :
[url=] [/url] [url=] [/url] [url=] [/url] [url=] [/url] [url=] [/url]

Thanks!

Sad61 评论于: 2007.06.27 19:24
除了楼主帖子,以上的回复,都是垃圾! [回复]

除了楼主帖子,以上的回复,都是垃圾!
一堆垃圾人乱扔的一堆垃圾! angry.gif

网络超级清洁工 评论于: 2007.07.25 11:27
[回复]

Vilyamkr 评论于: 2007.10.01 21:37
[回复]

Vilyamkh 评论于: 2007.10.01 21:37
[回复]

Vilyamnc 评论于: 2007.10.01 21:37
[回复]

Vilyamnc 评论于: 2007.10.01 21:37
Beautiful portal! nrmam [回复]

Beautiful portal! nrmam

ontsg 评论于: 2009.05.24 13:43
I like this work! thnko [回复]

I like this work! thnko

kwofl 评论于: 2009.05.24 13:49
sdff [回复]

fdss

ss 评论于: 2009.07.14 09:33
Wanted to say hi [回复]

Hi all wanted to introduce myself!! I look forward to being part of this comunity.

immishsoush 评论于: 2009.11.06 10:40

发表评论
标题

在此添加评论
表情符号: smile laughing tongue angry crying sad wassat wink

称呼

邮箱地址(可选)

个人主页(可选)




自我介绍
切换风格
新闻聚合
博客日历
文章归档...
最新发表...
博客统计...
Blog信息
网站链接...