所有的MIS系統都存在一個同樣的需求
MERGE INTO [your table
USING
(
[write your query here]
)[rename your query
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 關鍵字
為了能夠使問題與實際問題更加貼切
DEFINE TABLE :
SCORE : using for save the students
STUDENTS : the base information of students
DEFINE COLUMNS :
STUNO : the students
STUNAME : students
COURSENAME : course name
COURSESCORE : the study
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
WHERE
A
AND A
A
)X
ON
(S
WHEN
MATHED
THEN
UPDATE SET COURSESCORE=X
WHEN
NOT MATHED
THEN
INSERT
(
STUNO
CLASSNAME
)
VALUES
(
X
X
);
注意到 MERGE 語句在最後的
這時
public yourMethod(statement
try{
Connection conn=
PreparedStatement ps=
Resultset rs=
conn
for(int i=
//add your code here !
ps
}
ps
mit();
}catch(Exception e){
try{
conn
}catch(Exception el){}
}
這時
Oracle 存儲過程定義格式如下
CREATE OR REPLACE PROCEDURE PRO_YOUR_PROCEDURE (
ELEMENT_
ELEMENT_
)
AS
ARGUMENT_
BEGIN
MERGE INTO YOUR_TABLE_NAEM [RENAEM_YOUR_TABLE_HERE]
END;
EXCEPTION
WHEN
OTHERS
THEN
RAISE_APPLICATION_ERROR(
END;
COMMIT;
END PRO_YOUR_PROCEDURE;
其中
接下來就是如何來在 Java 程序中調用你的存儲過程
核心代碼如下
OracleCallableStatement cal = null;
cal=(OracleCallableStatement)conn
for(………………){
…………
cal
…………
cal
}
From:http://tw.wingwit.com/Article/program/Oracle/201311/18149.html