前天有网友问题这个问题,今天测试一把,需求应该是如果producre存储过程运行出错,可以写错误日志到OS的文本文件,这样系统管理员就可以看到,无需分配数据库权限.
eg.
eg.
SQL> create directory dir_log as '/oracle10g/log';
Directory created.
SQL> grant read,write on directory dir_log to anbob;
Grant succeeded.
SQL> conn anbob/anbob;
Connected.
SQL> create table test_log (id int,name varchar2(20));
Table created.
SQL> insert into test_log values(1,'anbob');
1 row created.
SQL> commit;
Commit complete.
SQL> create or replace procedure pro_test(p_id number)
2 is
3 v_name varchar2(20);
4 begin
5 select name into v_name from test_log where id=p_id;
6 dbms_output.put_line(v_name);
7 exception
8 when no_data_found then
9 dbms_output.put_line('a');
10 end;
11 /
Procedure created.
SQL> set serveroutput on
SQL> exec pro_test(1);
anbob
PL/SQL procedure successfully completed.
SQL> exec pro_test(2);
a
PL/SQL procedure successfully completed.
SQL> l
1 create or replace procedure pro_test(p_id number)
2 is
3 v_name varchar2(20);
4 v_fil utl_file.file_type;
5 begin
6 select name into v_name from test_log where id=p_id;
7 dbms_output.put_line(v_name);
8 exception
9 when no_data_found then
10 v_fil := utl_file.fopen('DIR_LOG','DB_PRO.LOG','W');
11 utl_file.PUT_LINE(v_fil,'ERROR:'||SYSDATE||'-'||sqlerrm);
12 UTL_FILE.fclose(v_fil);
13* end;
SQL> /
Procedure created.
SQL> exec pro_test(1);
anbob
PL/SQL procedure successfully completed.
SQL> exec pro_test(2);
PL/SQL procedure successfully completed.
SQL> host
[oracle@anbob ~]$ cd /oracle10g/log/
[oracle@anbob log]$ ll
total 4
-rw-r--r-- 1 oracle oinstall 42 Mar 9 05:04 DB_PRO.LOG
[oracle@anbob log]$ cat DB_PRO.LOG
ERROR:09-3 -12-ORA-01403: no data found
[oracle@anbob log]$复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
Oracle DataGuard高可用性解决方案详解
孙莹
589次阅读
2025-03-26 23:27:33
Oracle RAC 一键安装翻车?手把手教你如何排错!
Lucifer三思而后行
542次阅读
2025-04-15 17:24:06
【纯干货】Oracle 19C RU 19.27 发布,如何快速升级和安装?
Lucifer三思而后行
456次阅读
2025-04-18 14:18:38
Oracle SQL 执行计划分析与优化指南
Digital Observer
442次阅读
2025-04-01 11:08:44
XTTS跨版本迁移升级方案(11g to 19c RAC for Linux)
zwtian
439次阅读
2025-04-08 09:12:48
【ORACLE】记录一些ORACLE的merge into语句的BUG
DarkAthena
437次阅读
2025-04-22 00:20:37
墨天轮个人数说知识点合集
JiekeXu
436次阅读
2025-04-01 15:56:03
【ORACLE】你以为的真的是你以为的么?--ORA-38104: Columns referenced in the ON Clause cannot be updated
DarkAthena
412次阅读
2025-04-22 00:13:51
Oracle数据库一键巡检并生成HTML结果,免费脚本速来下载!
陈举超
402次阅读
2025-04-20 10:07:02
Oracle 19c RAC更换IP实战,运维必看!
szrsu
391次阅读
2025-04-08 23:57:08
热门文章
移除DataGuard Standby配置导致Primary启动失败
2023-08-17 21293浏览
使用dblink产生的”SELECT /*+ FULL(P) +*/ * FROM XXXXX P ” 解析
2023-06-20 20893浏览
Troubleshooting 'ORA-28041: Authentication protocol internal error' change password 12c R2 DB
2020-04-08 13643浏览
浅谈ORACLE免费数据库Oracle Database XE (Express Edition) 版
2018-10-31 7590浏览
High wait event ‘row cache mutex’ in 12cR2、19c
2020-08-14 5570浏览