有關oracle預存程序的6個問題

來源:互聯網
上載者:User
皓月蒼狼 2017-11-27 23:17:37

在oracle中,資料表別名不能加as,如:

select a.appname from appinfo a;--正確;

select a.appname from appinfo as a;--錯誤

可能與oracle中的預存程序中的關鍵字as發生衝突

在預存程序中,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,編譯報錯;

在利用select ...into..文法時,必須先確保資料庫中有該條記錄,否則會報出"no data found"異常。

可以在該文法之前,先利用select count(*) from 查看資料庫中是否存在該記錄,如果存在,再利用select...into...

4.在預存程序中,別名不能和欄位名稱相同,否則雖然編譯可以通過,但在運行階段會報錯;

5.在預存程序中,關於出現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='xxxxxxx';

如果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.getHibernateTemplete().execute(

new HibernateCallback(){

public Object doInHinernate(Session session)

throws HibernateException,SQLException{

CallableStatement cs=session.connection().prepareCall("{call modifyapppnumber_remain(?)}");

cs.setString(1,foundationid);

cs.execute();

return null;

}

} );

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.