oracle sqlcode 多条,oracle sqlerrm和sqlcode函数
1.oracle sqlcode函數(shù)
sqlcode函數(shù)用來(lái)返回pl/sql執(zhí)行內(nèi)部異常代碼。
語(yǔ)法:sqlcode
oracle sqlerrm函數(shù)
sqlerrm函數(shù)返回指定錯(cuò)誤代碼的錯(cuò)誤信息。
語(yǔ)法:SQLERRM [(error_number)]
2.sqlerrm如何使用?
sqlcode和sqlerrm是不能直接在sql語(yǔ)句中使用,必須先將其賦給變量后,才能在sql語(yǔ)句中使用,如下:
dw@dw>declare
2??? v_sqlcode number;
3??? v_sqlcode1 number;
4??? v_sqlerrm varchar2(510);
5? begin
6??? v_sqlcode :=sqlcode;
7??? select v_sqlcode into v_sqlcode1 from dual;
8? end;
9? /
PL/SQL 過(guò)程已成功完成。
sqlerrm用得最多的是在pl/sql編程中來(lái)捕獲異常的詳細(xì)信息,特別是在when others 中使用非有用。
如下:
view plaincopy to clipboardprint?
set serveroutput on
declare
v_count pls_integer;
begin
begin
select 2 into v_count
from dual where 1>2;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
begin
select level into v_count
from dual
connect by level < 3;
end;
end;
/
dw@dw>set serveroutput on
dw@dw>declare
2??? v_count pls_integer;
3? begin
4??? begin
5????? select 2 into v_count
6????? from dual where 1>2;
7???? exception
8?????? when others then
9???????? dbms_output.put_line(sqlerrm);
10??? end;
11??? begin
12????? select level into v_count
13????? from dual
14????? connect by level < 3;
15??? end;
16? end;
17? /
ORA-01403: 未找到任何數(shù)據(jù)
declare
*
第 1 行出現(xiàn)錯(cuò)誤:
ORA-01422: 實(shí)際返回的行數(shù)超出請(qǐng)求的行數(shù)
ORA-06512: 在 line 12
3.sqlerrm的最大長(zhǎng)度是多少?
其實(shí)這個(gè)問(wèn)題很好解答,自定義一個(gè)異常,然后用sqlerrm去捕獲即可。
view plaincopy to clipboardprint?
set serveroutput on
begin
raise_application_error(-20001,
'sqlerrm的最大長(zhǎng)度是多少' || lpad('我', 10000, '我'));
exception
when others then
dbms_output.put_line(lengthb(sqlerrm));
end;
/
dw@dw>begin
2??? raise_application_error(-20001,
3??????????????????????????? 'sqlerrm的最大長(zhǎng)度是多少' || lpad('我', 10000, '我'));
4? exception
5??? when others then
6????? dbms_output.put_line(lengthb(sqlerrm));
7? end;
8? /
510
PL/SQL 過(guò)程已成功完成。
dw@dw>
從輸出結(jié)果來(lái)看,oracle sqlerrm的最在長(zhǎng)度為510個(gè)字節(jié)。
4.oracle ora-錯(cuò)誤信息手冊(cè)
從oracle的參考文檔中,可以發(fā)現(xiàn)自定義異常從-20000開(kāi)始,也就是說(shuō),-20000前的數(shù)據(jù)都是oracle內(nèi)部使用的,
可以通過(guò)下面這段匿名塊查出oracle所有ora-錯(cuò)誤信息。
view plaincopy to clipboardprint?
set serveroutput on
exec dbms_output.enable(10000000);
begin
for i in 0..20000 loop
dbms_output.put_line(sqlerrm(-i));
end loop;
end;
/
來(lái)自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/26953882/viewspace-723344/,如需轉(zhuǎn)載,請(qǐng)注明出處,否則將追究法律責(zé)任。
總結(jié)
以上是生活随笔為你收集整理的oracle sqlcode 多条,oracle sqlerrm和sqlcode函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cornerstone 加载dicom图
- 下一篇: 基于预计算的全局光照技术