Oracle 預存程序

來源:互聯網
上載者:User

1.基本結構
CREATE OR REPLACE PROCEDURE 預存程序名字
(
    參數1 IN NUMBER,
    參數2 IN NUMBER
) IS
變數1 INTEGER :=0;
變數2 DATE;
BEGIN

END 預存程序名字

2.SELECT INTO STATEMENT
將select查詢的結果存入到變數中,可以同時將多個列儲存多個變數中,必須有一條
記錄,否則拋出異常(如果沒有記錄拋出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 迴圈
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.帶參數的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
串連資料庫後建立一個Test WINDOW
在視窗輸入調用SP的代碼,F9開始debug,CTRL+N單步調試

關於Oracle預存程序的若干問題備忘1.在oracle中,資料表別名不能加as,如:
select a.appname from appinfo a;-- 正確
select a.appname from appinfo as a;--錯誤
也許,是怕和oracle中的預存程序中的關鍵字as衝突的問題吧
2.在預存程序中,select某一欄位時,後面必須緊跟into,如果select整個記錄,利用遊標的話就另當別論了。
  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,編譯報錯,提示:Compilation
   Error: PLS-00428: an INTO clause is expected in this SELECT statement

3.在利用select...into...文法時,必須先確保資料庫中有該條記錄,否則會報出"no data found"異常。
   可以在該文法之前,先利用select count(*) from 查看資料庫中是否存在該記錄,如果存在,再利用select...into...
4.在預存程序中,別名不能和欄位名稱相同,否則雖然編譯可以通過,但在運行階段會報錯
select keynode into kn from APPFOUNDATION where appid=aid and foundationid=fid;-- 正確運行
select af.keynode into kn from APPFOUNDATION af where af.appid=appid and af.foundationid=foundationid;-- 運行階段報錯,提示
ORA-01422:exact fetch returns more than requested number of rows5.在預存程序中,關於出現null的問題
假設有一個表A,定義如下:
create table A(
id varchar2(50) primary key not null,
vcount number(8) not null,
bid varchar2(50) not null -- 外鍵
);如果在預存程序中,使用如下語句:
select sum(vcount) into fcount from A where bid='xxxxxx';如果A表中不存在bid="xxxxxx"的記錄,則fcount=null(即使fcount定義時設定了預設值,如:fcount number(8):=0依然無效,fcount還是會變成null),這樣以後使用fcount時就可能有問題,所以在這裡最好先判斷一下:
if fcount is null then
     fcount:=0;
end if;這樣就一切ok了。
6.Hibernate調用oracle預存程序
        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;
                     }
                 });

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.