oracle sqlcode 多条,oracle sqlerrm和sqlcode函数
1.oracle sqlcode函數
sqlcode函數用來返回pl/sql執行內部異常代碼。
語法:sqlcode
oracle sqlerrm函數
sqlerrm函數返回指定錯誤代碼的錯誤信息。
語法:SQLERRM [(error_number)]
2.sqlerrm如何使用?
sqlcode和sqlerrm是不能直接在sql語句中使用,必須先將其賦給變量后,才能在sql語句中使用,如下:
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 過程已成功完成。
sqlerrm用得最多的是在pl/sql編程中來捕獲異常的詳細信息,特別是在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: 未找到任何數據
declare
*
第 1 行出現錯誤:
ORA-01422: 實際返回的行數超出請求的行數
ORA-06512: 在 line 12
3.sqlerrm的最大長度是多少?
其實這個問題很好解答,自定義一個異常,然后用sqlerrm去捕獲即可。
view plaincopy to clipboardprint?
set serveroutput on
begin
raise_application_error(-20001,
'sqlerrm的最大長度是多少' || lpad('我', 10000, '我'));
exception
when others then
dbms_output.put_line(lengthb(sqlerrm));
end;
/
dw@dw>begin
2??? raise_application_error(-20001,
3??????????????????????????? 'sqlerrm的最大長度是多少' || lpad('我', 10000, '我'));
4? exception
5??? when others then
6????? dbms_output.put_line(lengthb(sqlerrm));
7? end;
8? /
510
PL/SQL 過程已成功完成。
dw@dw>
從輸出結果來看,oracle sqlerrm的最在長度為510個字節。
4.oracle ora-錯誤信息手冊
從oracle的參考文檔中,可以發現自定義異常從-20000開始,也就是說,-20000前的數據都是oracle內部使用的,
可以通過下面這段匿名塊查出oracle所有ora-錯誤信息。
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;
/
來自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/26953882/viewspace-723344/,如需轉載,請注明出處,否則將追究法律責任。
總結
以上是生活随笔為你收集整理的oracle sqlcode 多条,oracle sqlerrm和sqlcode函数的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: cornerstone 加载dicom图
- 下一篇: 【备战秋招系列-3】Java高频知识点—
