(&變量名稱:運行時輸入的變量)
中文亂碼解決:
--查看系統環境變量
select * from nls_database_parameters;
NLS_LANGUAGE.NLS_TERRITORY.NLS_CHARACTERSET
編輯配置文件:
export LANG=zh_CN.utf8
export NLS_LANG=AMERICAN.AMERICA.WEBMSWIN1252
export SQLPATH=/home/oracle
export EDITOR=vi
export sqlplus='rlwrap sqlplus'
使修改后的配置文件立即生效:. !$
處理預定義異常:
declare
t_name varchar2(50);
begin
select user_name into t_name from t_user where user_code=$t_usercode;
dbms_output.put_line(t_name);
exception
when no_data_found then
dbms_output.put_line('未查詢到數據!');
when too_many_rows then
dbms_output.put_line('向標量填充太多元素!');
end;
捕獲oracle錯誤(定義異常類型的變量,與oracle錯誤代碼關聯)
declare
own_error exception;
pragma exception_init(own_error, -2291);
begin
update t_user set user_name=&t_name where user_code=&t_usercode;
exception
when own_error then
dbms_output.put_line('主鍵不存在!');
end;
/
使用others捕獲所有沒有考慮到的異常:
declare
t_flag char(1);
t_usercode number:=&m_usercode;
begin
select 'C' indo t_flag from t_user where user_code=t_usercode;
exception
when others then
dbms_output.put_line(sqlcode||'--'||sqlerrm);--sqlcode表示異常代碼,sqlerrm表示異常信息
end;
/
(除了100--ORA-01403: no data found,所有的錯誤代碼都是ORA后面跟的數字)
自定義異常:ORA-20000 ~ ORA-20999(oracle提供的自定義異常代碼取值范圍)
declare
begin
raise_application_error(-20000, '不能修改人員代碼‘);
update t_user set user_code=1 where user_name='老大';
end;
/
declare
own_error exception;
pragma exception_init(own_error, -20000);
begin
if to_char(sysdate, 'DY') in ('SAT', 'SUN') then
raise_application_error(-20000, '周末必須休息!');
else
update t_user set apple=apple+3;
end if;
exception
when own_error then
dbms_output.put_line(sqlcode||'--'||sqlerrm);
end;
/