Oracle:PL*Plus編程(三)

異常

一.常用異常

異常名稱 錯誤代碼 描述
ACCESS_INTO_NULL ORA-06530 試圖對未初始化對象屬性賦值。
CASE_NOT_FOUNT ORA-06592 未在case語句中找到匹配的when子句,也沒有默認的else子句
INVALID_CURSOR ORA-01001 程序試圖進行非法遊標操作
INVALID_NUMBER ORA-01722 試圖將字符串轉換成數字時失敗,因爲字符串並不代表有效的數字
LOGIN_DENIED ORA-01017 試圖用非法的用戶名或密碼連接數據庫
TOO_MANAY_ROWS ORA-01422 select into 語句返回多行
DUP_VAL_ON_INDEX ORA-00001 試圖向唯一索引約束的列中插入重複值。

例子:

begin 
    dbms_output.put_line(1/0);
exception
     when zero_divide then 
end;

結果:

Division by zero

OTHERS:可以處理所有異常
例如:

begin 
    dbms_output.put_line(1/0);
exception 
    when others then dbms_output.put_line('Division by zero');
end;

結果:

Division by zero

過程

二.過程

1.創建過程

語法:

CREATE [OR REPLACE] PROCEDURE procedure_name
{[parameter_name [IN | OUT | IN OUT] type [, ...]}
{IS | AS}
BEGIN
procedure_body
END procedure_name;

例如:

create procedure update_test_type(
    test_id in test.id%TYPE,
    test_type in varchar(10) 
) as
    v_count integer;
begin
    select count(*)
    into v_count
    from test
    where id = test_id;
    if v_count = 1 then
        update test
        set type = test_type
        where id = test_id;
        commit;
    end if;
exception
    when others then
        rollback;
end update_test_type;

2.調用過程

call update_test_type(1, '09');

3.獲取有關過程的信息

select object_name, aggregate, parallel
from user_procedures
where object_name='update_test_type';

4.刪除過程

drop procedure update_test_type;

5.查看錯誤

show errors
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章