oralce 异常处理 exception
oracle?10g:?PL/SQL?User's?Guide?and?Reference
?--->?10?Handling?PL/SQL?Errors
?--->?Summary?of?Predefined?PL/SQL?Exceptions?
系統(tǒng)預(yù)定義異常(有名字的錯(cuò)誤代碼):
TOO_MANY_ROWS?:?SELECT?INTO返回多行
INVALID_CURSOR?:非法指針操作(關(guān)閉已經(jīng)關(guān)閉的游標(biāo))
ZERO_DIVIDE?:除數(shù)等于零
DUP_VAL_ON_INDEX?:違反唯一性約束
ACCESS_INTO_NULL:?未定義對(duì)象?
CASE_NOT_FOUND:?CASE?中若未包含相應(yīng)的?WHEN?,并且沒(méi)有設(shè)置?ELSE?時(shí)?
COLLECTION_IS_NULL:?集合元素未初始化?
CURSER_ALREADY_OPEN:?游標(biāo)已經(jīng)打開(kāi)?
DUP_VAL_ON_INDEX:?唯一索引對(duì)應(yīng)的列上有重復(fù)的值?
INVALID_NUMBER:?內(nèi)嵌的?SQL?語(yǔ)句不能將字符轉(zhuǎn)換為數(shù)字?
NO_DATA_FOUND:?使用?select?into?未返回行,或應(yīng)用索引表未初始化的元素時(shí)?
SUBSCRIPT_BEYOND_COUNT:元素下標(biāo)超過(guò)嵌套表或?VARRAY?的最大值?
SUBSCRIPT_OUTSIDE_LIMIT:?使用嵌套表或?VARRAY?時(shí),將下標(biāo)指定為負(fù)數(shù)??
VALUE_ERROR:?賦值時(shí),變量長(zhǎng)度不足以容納實(shí)際數(shù)據(jù)?
LOGIN_DENIED:?PL/SQL?應(yīng)用程序連接到?oracle?數(shù)據(jù)庫(kù)時(shí),提供了不正確的用戶(hù)名或密碼?
NOT_LOGGED_ON:?PL/SQL?應(yīng)用程序在沒(méi)有連接?oralce?數(shù)據(jù)庫(kù)的情況下訪(fǎng)問(wèn)數(shù)據(jù)?
PROGRAM_ERROR:?PL/SQL?內(nèi)部問(wèn)題,可能需要重裝數(shù)據(jù)字典&?pl./SQL?系統(tǒng)包?
ROWTYPE_MISMATCH:?宿主游標(biāo)變量與?PL/SQL?游標(biāo)變量的返回類(lèi)型不兼容?
SELF_IS_NULL:?使用對(duì)象類(lèi)型時(shí),在?null?對(duì)象上調(diào)用對(duì)象方法?
STORAGE_ERROR:?運(yùn)行?PL/SQL?時(shí),超出內(nèi)存空間?
SYS_INVALID_ID:?無(wú)效的?ROWID?字符串?
TIMEOUT_ON_RESOURCE:?Oracle?在等待資源時(shí)超時(shí)?
oracl?11g:?Pl/SQL?LANGUAGE?REFERENCE
?---->?11??PL/SQL?error?handling
?---->?Predefined?Exceptions
練習(xí)?1:捕獲預(yù)定義異常
declare
??v1?emp.sal%type;
begin
??select?sal?into?v1?from?emp;
end;
/
declare
??v1?emp.sal%type;
begin
??select?sal?into?v1?from?emp;
exception
??when?TOO_MANY_ROWS?then
???dbms_output.put_line(sqlcode||';'||sqlerrm);
end;
/
declare
???v_ename?employees.last_name%type;
begin
???select?last_name?into?v_ename?from?employees;
exception
???when?too_many_rows?then
????????dbms_output.put_line('too?many?rows,you?should?add?a?condition');
end;
/
練習(xí)?2:捕獲預(yù)定義異常
declare
??v1?emp.sal%type;
begin
??select?sal?into?v1?from?emp?where?empno=7777;
exception
??when?TOO_MANY_ROWS?then
????dbms_output.put_line('more?person?!');
--??when?NO_DATA_FOUND?then
--????dbms_output.put_line('no?rows?selected!');
??when?others?then?--other執(zhí)行器
????dbms_output.put_line(sqlcode||';'||sqlerrm);
end;
/
declare
???v_sal?hr.employees.salary%type;
begin
???select?salary?into?v_sal?from?hr.employees?where?department_id=?&ID;
???dbms_output.put_line(v_sal);
exception
???WHEN?TOO_MANY_ROWS?then
????????dbms_output.put_line('there?are?too?many?rows?to?return.');
???when?NO_DATA_FOUND?THEN
????????DBMS_OUTPUT.PUT_LINE('there?is?no?data?to?return.');
end;
declare
???v_ename?employees.last_name%type;
begin
???select?last_name?into?v_ename?from?employees;
exception
???when?too_many_rows?then
????????dbms_output.put_line('sqlcode?:'||sqlcode?||chr(10)||'sqlerrm?:'||sqlerrm);
end;
/
sqlcode?:-1422
sqlerrm?:ORA-01422:?exact?fetch?returns?more?than?requested?number?of?rows
練習(xí)?3:捕獲錯(cuò)誤代碼和錯(cuò)誤描述,借助預(yù)定義函數(shù)sqlcode(ERROR代碼),sqlerrm(ERROR文本)
begin
??update?emp?set?deptno=60;
??dbms_output.put_line('ok');
exception
??when?TOO_MANY_ROWS?then
????dbms_output.put_line('more?person?!');
??when?NO_DATA_FOUND?then
????dbms_output.put_line('no?person?!');
??when?others?then?--other執(zhí)行器
????dbms_output.put_line(sqlcode||';'||sqlerrm);
end;
/
練習(xí)?4:捕獲非預(yù)定義異常(捕獲oracle錯(cuò)誤代碼)
declare
??fk_error??exception;--聲明異常
??pragma?exception_init(fk_error,-2292);--使用編譯指示器將異常名稱(chēng)和oracle的錯(cuò)誤代碼綁定
begin
??delete?dept;?--oracle自動(dòng)傳播錯(cuò)誤(fk_error)
??dbms_output.put_line('ok');
exception
??when?TOO_MANY_ROWS?then
????dbms_output.put_line('more?person?');
??when?NO_DATA_FOUND?then
????dbms_output.put_line('no?person?');
??when?fk_error?then
????dbms_output.put_line('infringe?forign?key?!');??
end;
/
declare
???pk_fk_error?exception;
???pragma?exception_init(pk_fk_error,-2292);
begin
???delete?hr.departments;
???dbms_output.put_line('It?had?been?deleted.');
exception
???when?pk_fk_error?then
????????dbms_output.put_line('It?can?not?delete.');
end;
/
It?can?not?delete.
用戶(hù)自定義異常的sqlcode代碼段:-20001?~?-20999
練習(xí)?5:捕獲用戶(hù)自定義的異常:
declare
??my_error?EXCEPTION;
??PRAGMA?EXCEPTION_INIT(my_error,?-20001);--編譯指示,將命名的異常與ORACLE?ERROR關(guān)聯(lián)
BEGIN
??raise_application_error(-20001,'工資不能被改動(dòng)!');--將異常傳送到環(huán)境
??UPDATE?e?SET?SAL=1000;
EXCEPTION
??WHEN?NO_DATA_FOUND?THEN
????DBMS_OUTPUT.PUT_LINE('未檢索到數(shù)據(jù)!');
??WHEN?TOO_MANY_ROWS?THEN
????DBMS_OUTPUT.PUT_LINE('SELECT返回多行數(shù)據(jù)!');
??WHEN?MY_ERROR?THEN
????DBMS_OUTPUT.PUT_LINE('E表工資不可以被修改!');
end;
/
declare
???my_excep?EXCEPTION;
???PRAGMA?EXCEPTION_init(my_excep,-20001);
???v_did?hr.employees.department_id%type?:=?&id;
begin
???if?v_did?!=?10?then
????????raise_application_error(-20001,'my?own?exception.');
???else
???????update?hr.employees?set?salary=salary+1?where?department_id=v_did;
???????dbms_output.put_line('ok');
???end?if;
end;
練習(xí)?6:捕獲用戶(hù)自定義的異常
declare
??my_error?EXCEPTION;
??PRAGMA?EXCEPTION_INIT(my_error,?-20001);
??v_empno?number(4):=&p_empno;
begin
??IF?TO_CHAR?(SYSDATE,?'HH24')?NOT?BETWEEN?'08'?AND?'14'?OR?TO_CHAR?(SYSDATE,?'DY')?IN?('星期六',?'星期日')?THEN
????RAISE?my_error;
??else
????insert?into?e(empno)?values?(v_empno);
????dbms_output.put_line('insert?成功!');
??END?IF;
exception
??when?my_error?then
????dbms_output.put_line('該時(shí)間段不能向E表插入數(shù)據(jù)!');
end;
/
declare
???v_raise_sal?number?:=?&raise_sal;
???my_error?EXCEPTION;
???PRAGMA?exception_init(my_error,-20001);
begin
???if?v_raise_sal?>?1000?then
??????raise_application_error(-20001,'can?not?raise?so?much?money.');
???else
??????update?employees?set?salary=salary+v_raise_sal;
???end?if;
--exception
--???when?my_error?then
--???????dbms_output.put_line('you?can?not?raise.');
end;
/
錯(cuò)誤報(bào)告:
ORA-20001:?can?not?raise?so?much?money.
ORA-06512:?at?line?7
****************************************************************************************************
練習(xí)?7:打印?ORA-#####?錯(cuò)誤編號(hào)和描述:
SPOOL?D:\ORACLE_ERROR.TXT
SET?SERVEROUTPUT?ON
DECLARE
??ERR_MSG?VARCHAR2(4000);
??ERR_CODE?NUMBER(10);
BEGIN
??DBMS_OUTPUT.ENABLE(1000000);
??FOR?ERR_NUM?IN?20000..20999
??LOOP
????ERR_CODE:=sqlcode;
????ERR_MSG?:=?SQLERRM(-ERR_NUM);
?????IF?ERR_MSG?NOT?LIKE?'%Message?'||ERR_NUM||'?not?found%'?then
???????dbms_output.put_line(ERR_MSG);
?????END?IF;
??END?LOOP;
END;
/
SPOOL?OFF;
--嵌套的PL/SQL代碼段的異常處理
declare
??v_ename?varchar2(10);
begin
??select?ename?into?v_ename?from?emp?where?empno=7839;
??dbms_output.put_line(v_ename);
????declare
??????v1?emp.sal%type;
????begin
??????select?sal?into?v1?from?emp;
????exception
??????when?TOO_MANY_ROWS?then
???????dbms_output.put_line(sqlcode||';'||sqlerrm);
????end;
??dbms_output.put_line('ok');
end;
/
--內(nèi)部pl/sql代碼段,進(jìn)行了異常處理,是所有外部程序可以正常執(zhí)行;
declare
???v_name?employees.last_name%type;
begin
???select?last_name?into?v_name?from?hr.employees
???where?employee_id=100;
???declare
???????v_no?departments.department_id%type;
???begin
???????select?department_id?into?v_no?from?departments;
???exception
??????when??OTHERS?then
??????????dbms_output.put_line('too?may?rows');
???end;
???dbms_output.put_line(v_name);
end;
/
too?may?rows
King
--內(nèi)部pl/sql代碼段異常
declare
???v_name?hr.employees.last_name%type;
begin
???select?last_name?into?v_name?from?hr.employees?where?employee_id=100;
???dbms_output.put_line(v_name);
???declare
???????v_sal?hr.employees.salary%type;
???begin
???????select?salary?into?v_sal?from?employees;
???????dbms_output.put_line('inner?pl/sql?block');
--???exception
--???????when?too_many_rows?then
--???????????dbms_output.put_line('inner?exception?capured.');
???end;
???dbms_output.put_line('outer?block');
exception
???when?too_many_rows?then
???????dbms_output.put_line('outer?exception?captured.');
end;
?
轉(zhuǎn)載于:https://blog.51cto.com/plking/1337345
總結(jié)
以上是生活随笔為你收集整理的oralce 异常处理 exception的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 创建视图,查询表空间的利用情况
- 下一篇: 大作文_p2_v1.0