Oracle存储过程小解
生活随笔
收集整理的這篇文章主要介紹了
Oracle存储过程小解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Oracle存儲過程小解
1.創(chuàng)建語法create or replace procedure pro_name(paramIn in type, paramOUt out type,paramInOut in out type)as(is)[類似于mysql的declare]beginstatement...end;注:<1>入?yún)⒉挥脤戦L度,只需寫類型,in、out、in out寫在參數(shù)名后,區(qū)別于mysql寫在參數(shù)名前,不寫默認(rèn)為in;<2>in是值傳遞,out和in out是引用傳遞,in的值不可修改,out的值在進入存儲過程是初始化null。<3>as(is)類似于mysql的declare,也可以在begin后使用declare定義變量;設(shè)置變量值,也不用set,直接name := value或者使用select...into...;<4>沒有類似于mysql中的變換分隔符,最后end;即可。<5>沒有入?yún)r,可不要pro_name后();
<6>變量聲明:在begin之前,直接varName type;在內(nèi)部,declare varName type.變量賦值需要使用 := 符號 2.常用方法<1> if...then...elseif...then...end if;<2>多種循環(huán):a.loop...exit when... end loop;b.while...loop....end loop;c.for...in...loop...end loop;(強烈推薦,mysql沒有for循環(huán))
d.exit可用于跳出循環(huán),return結(jié)束存儲過程
e.<<loopName>>...goto loopName:類似于標(biāo)記; <3>游標(biāo)cursor:游標(biāo)屬性:cursor%found; --有數(shù)據(jù)cursor%notfound; --無數(shù)據(jù)cursor%isopen; --游標(biāo)已開啟cursor%rowcount; --受最后SQL語句影響的行數(shù) 3.異常處理<1>.通過關(guān)鍵字exception捕獲異常語法:exceptionwhen exception_decription thenstatemntwhen exception_description2 thenstatementwhen others thenstatement<2>.最常用的異常:no_data_found:select into語句沒有數(shù)據(jù);too_many_rows:select into有多條數(shù)據(jù);dup_val_on_index:唯一索引列重復(fù);storage_error:內(nèi)存溢出;zero_devide:除數(shù)為0;case_not_found:case沒有匹配的條件且沒有else;cursor_already_open:游標(biāo)已打開;timeout_on_resource:請求資源超時。<3>.自定義異常:(類似于mysql的自定義condition,避免error_code值帶來的閱讀性太差的問題);progma exception_init(selfexception,-oracle_error_code);示例:declare demo_exception exception;progma exception_init(demo_exception,-60);<4>.處理異常a.不拋出,statement處理;b.拋出異常:●存儲過程自動拋出●通過raise關(guān)鍵字拋出,如 raise no_data_found;●通過raise_application_error(error_number,message[flag(true,false)]);error_number數(shù)值范圍從-20999到-20000;messgae表示異常描述;flag表示是添加到(true)或者覆蓋(false)錯誤堆,默認(rèn)是false;如:raise_application_error(-20001,'invalid id number');<5>異常處理機制與java異常處理機制相似。 4.常用技巧:<1>execute immediate statement to param;關(guān)鍵字:execute immediate...to...;它解析并馬上執(zhí)行動態(tài)的SQL語句或非運行時創(chuàng)建的PL/SQL塊,可以理解為執(zhí)行動態(tài)SQL。注意幾點:a.不支持返回多行的操作,這種情況應(yīng)該用refcursor來處理b.執(zhí)行sql時不要加分好,pl/sql塊時加分號;c.使用之前應(yīng)該將之前的事務(wù)顯示提交。示例:execute immediate 'select dname, loc from dept where deptno = :1'into l_nam, l_locusing l_dept ;<2>sys_refscursor:非正常游標(biāo),用于返回結(jié)果集示例:create or replace procedure up_test(o out sys_refcursor) is --可以在代碼中獲取返回值beginopen o for select * from lq_test;end;<3>%type作用:與關(guān)聯(lián)表的關(guān)聯(lián)字段類型長度綁定起來,跟隨綁定表字段的變化,是一種非常好的變成習(xí)慣,避免多次更改:示例:declare v_name students.name%type;<4>%rowtype表示該列為行數(shù)據(jù)類型,存儲的為一行數(shù)據(jù),相當(dāng)于一條record相對于查詢結(jié)果或者游標(biāo)。作用:當(dāng)查詢一行數(shù)據(jù)時,比多個字段采用%type效率要高一些。示例:declare v_emp emp%rowtype;cursor cursor_name is select...from table...open cursor_namefor xxx in cursor_name loopv_emp := xxx;end loop;end cursor_name;
<6>變量聲明:在begin之前,直接varName type;在內(nèi)部,declare varName type.變量賦值需要使用 := 符號 2.常用方法<1> if...then...elseif...then...end if;<2>多種循環(huán):a.loop...exit when... end loop;b.while...loop....end loop;c.for...in...loop...end loop;(強烈推薦,mysql沒有for循環(huán))
d.exit可用于跳出循環(huán),return結(jié)束存儲過程
e.<<loopName>>...goto loopName:類似于標(biāo)記; <3>游標(biāo)cursor:游標(biāo)屬性:cursor%found; --有數(shù)據(jù)cursor%notfound; --無數(shù)據(jù)cursor%isopen; --游標(biāo)已開啟cursor%rowcount; --受最后SQL語句影響的行數(shù) 3.異常處理<1>.通過關(guān)鍵字exception捕獲異常語法:exceptionwhen exception_decription thenstatemntwhen exception_description2 thenstatementwhen others thenstatement<2>.最常用的異常:no_data_found:select into語句沒有數(shù)據(jù);too_many_rows:select into有多條數(shù)據(jù);dup_val_on_index:唯一索引列重復(fù);storage_error:內(nèi)存溢出;zero_devide:除數(shù)為0;case_not_found:case沒有匹配的條件且沒有else;cursor_already_open:游標(biāo)已打開;timeout_on_resource:請求資源超時。<3>.自定義異常:(類似于mysql的自定義condition,避免error_code值帶來的閱讀性太差的問題);progma exception_init(selfexception,-oracle_error_code);示例:declare demo_exception exception;progma exception_init(demo_exception,-60);<4>.處理異常a.不拋出,statement處理;b.拋出異常:●存儲過程自動拋出●通過raise關(guān)鍵字拋出,如 raise no_data_found;●通過raise_application_error(error_number,message[flag(true,false)]);error_number數(shù)值范圍從-20999到-20000;messgae表示異常描述;flag表示是添加到(true)或者覆蓋(false)錯誤堆,默認(rèn)是false;如:raise_application_error(-20001,'invalid id number');<5>異常處理機制與java異常處理機制相似。 4.常用技巧:<1>execute immediate statement to param;關(guān)鍵字:execute immediate...to...;它解析并馬上執(zhí)行動態(tài)的SQL語句或非運行時創(chuàng)建的PL/SQL塊,可以理解為執(zhí)行動態(tài)SQL。注意幾點:a.不支持返回多行的操作,這種情況應(yīng)該用refcursor來處理b.執(zhí)行sql時不要加分好,pl/sql塊時加分號;c.使用之前應(yīng)該將之前的事務(wù)顯示提交。示例:execute immediate 'select dname, loc from dept where deptno = :1'into l_nam, l_locusing l_dept ;<2>sys_refscursor:非正常游標(biāo),用于返回結(jié)果集示例:create or replace procedure up_test(o out sys_refcursor) is --可以在代碼中獲取返回值beginopen o for select * from lq_test;end;<3>%type作用:與關(guān)聯(lián)表的關(guān)聯(lián)字段類型長度綁定起來,跟隨綁定表字段的變化,是一種非常好的變成習(xí)慣,避免多次更改:示例:declare v_name students.name%type;<4>%rowtype表示該列為行數(shù)據(jù)類型,存儲的為一行數(shù)據(jù),相當(dāng)于一條record相對于查詢結(jié)果或者游標(biāo)。作用:當(dāng)查詢一行數(shù)據(jù)時,比多個字段采用%type效率要高一些。示例:declare v_emp emp%rowtype;cursor cursor_name is select...from table...open cursor_namefor xxx in cursor_name loopv_emp := xxx;end loop;end cursor_name;
?
轉(zhuǎn)載于:https://www.cnblogs.com/angry-scholar/p/7119501.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Oracle存储过程小解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jmeter测试元件--控制器
- 下一篇: 51nod1307(暴力树剖/二分dfs