皓月蒼狼 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;
}
} );