Oracle存儲(chǔ)過程的基本語法及注意事項(xiàng)
Oracle數(shù)據(jù)庫的存儲(chǔ)是Oracle數(shù)據(jù)庫重要的應(yīng)用之一,Oracle數(shù)據(jù)庫存儲(chǔ)的基本語法是完成Oracle存儲(chǔ)是基礎(chǔ),下文中將為大家講述Oracle存儲(chǔ)過程中的基本語法和注意事項(xiàng)。
1.基本結(jié)構(gòu)
CREATE OR REPLACE PROCEDURE 存儲(chǔ)過程名字
(
參數(shù)1 IN NUMBER,
參數(shù)2 IN NUMBER
) IS
變量1 INTEGER :=0;
變量2 DATE;
BEGIN
END 存儲(chǔ)過程名字
2.SELECT INTO STATEMENT
將select查詢的結(jié)果存入到變量中,可以同時(shí)將多個(gè)列存儲(chǔ)多個(gè)變量中,必須有一條
記錄,否則拋出異常(如果沒有記錄拋出NO_DATA_FOUND)
例子:
BEGIN
SELECT col1,col2 into 變量1,變量2 FROM typestruct where xxx;
EXCEPTION
WHEN NO_DATA_FOUND THEN
xxxx;
END;
...
3.IF 判斷
IF V_TEST=1 THEN
BEGIN
do something
END;
END IF;
4.while 循環(huán)
WHILE V_TEST=1 LOOP
BEGIN
XXXX
END;
END LOOP;
5.變量賦值
V_TEST := 123;
6.用for in 使用cursor
...
IS
CURSOR cur IS SELECT * FROM xxx;
BEGIN
FOR cur_result in cur LOOP
BEGIN
V_SUM :=cur_result.列名1+cur_result.列名2
END;
END LOOP;
END;
7.帶參數(shù)的cursor
CURSOR C_USER(C_ID NUMBER) IS SELECT NAME FROM USER WHERE TYPEID=C_ID;
OPEN C_USER(變量值);
LOOP
FETCH C_USER INTO V_NAME;
EXIT FETCH C_USER%NOTFOUND;
do something
END LOOP;
CLOSE C_USER;
8.用pl/sql developer debug
連接數(shù)據(jù)庫后建立一個(gè)Test WINDOW
在窗口輸入調(diào)用SP的代碼,F9開始debug,CTRL+N單步調(diào)試
關(guān)于oracle存儲(chǔ)過程的若干問題備忘
1.在oracle中,數(shù)據(jù)表別名不能加as,如:
select a.appname from appinfo a;-- 正確
select a.appname from appinfo as a;-- 錯(cuò)誤
也許,是怕和oracle中的存儲(chǔ)過程中的關(guān)鍵字as沖突的問題吧
2.在存儲(chǔ)過程中,select某一字段時(shí),后面必須緊跟into,如果select整個(gè)記錄,利用游標(biāo)的話就另當(dāng)別論了。
select af.keynode into kn from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 有into,正確編譯
select af.keynode from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 沒有into,編譯報(bào)錯(cuò),提示:Compilation
Error: PLS-00428: an INTO clause is expected in this SELECT statement
3.在利用select...into...語法時(shí),必須先確保數(shù)據(jù)庫中有該條記錄,否則會(huì)報(bào)出"no data found"異常。
可以在該語法之前,先利用select count(*) from 查看數(shù)據(jù)庫中是否存在該記錄,如果存在,再利用select...into...
4.在存儲(chǔ)過程中,別名不能和字段名稱相同,否則雖然編譯可以通過,但在運(yùn)行階段會(huì)報(bào)錯(cuò)
select keynode into kn from APPFOUNDATION where appid=aid and foundationid=fid;-- 正確運(yùn)行
select af.keynode into kn from APPFOUNDATION af where af.appid=appid and af.foundationid=foundationid;-- 運(yùn)行階段報(bào)錯(cuò),提示
ORA-01422:exact fetch returns more than requested number of rows
5.在存儲(chǔ)過程中,關(guān)于出現(xiàn)null的問題
假設(shè)有一個(gè)表A,定義如下:
create table A(
id varchar2(50) primary key not null,
vcount number(8) not null,
bid varchar2(50) not null -- 外鍵
);
如果在存儲(chǔ)過程中,使用如下語句:
select sum(vcount) into fcount from A where bid='xxxxxx';
如果A表中不存在bid="xxxxxx"的記錄,則fcount=null(即使fcount定義時(shí)設(shè)置了默認(rèn)值,如:fcount number(8):=0依然無效,fcount還是會(huì)變成null),這樣以后使用fcount時(shí)就可能有問題,所以在這里最好先判斷一下:
if fcount is null then
fcount:=0;
end if;
這樣就一切ok了。
6.Hibernate調(diào)用oracle存儲(chǔ)過程
this.pnumberManager.getHibernateTemplate().execute(
new HibernateCallback() ...{
public Object doInHibernate(Session session)
throws HibernateException, SQLException ...{
CallableStatement cs = session
.connection()
.prepareCall("{call modifyapppnumber_remain(?)}");
cs.setString(1, foundationid);
cs.execute();
return null;
}
通過上文的介紹,大家按照上文中講解的就能夠?qū)崿F(xiàn)Oracle數(shù)據(jù)庫存儲(chǔ),相信Oracle數(shù)據(jù)庫存儲(chǔ)過程中的基本語法對大家日后的工作是非常有幫助的,另外注意事項(xiàng)也是必須要掌握的,希望上文中講到的內(nèi)容對大家能夠有所幫助。