oracle异常处理
(&变量名称:运行时输入的变量) 中文乱码解决: --查看系统环境变量 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; / (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |