oracle 通過觸發器和儲存過程寫檔案到磁碟

2021-06-11 16:28:21 字數 1708 閱讀 1480

專案有乙個需求,需要監控一張表的資料變動,然後根據變動結果生成乙個檔案到磁碟便於日誌抓取程式進行監控。

需要用到oracle 自帶的util_file 包。

目標資料庫使用者為ceps.

用sysdba登陸,為使用者賦權:

grant resource to ceps;

create or replace directory batchdir as 'e:\ceps';

grant read,write on directory batchdir to ceps;

grant execute on utl_file to ceps;

grant create procedure to ceps;

grant create trigger to ceps;

接著是需要用到的過程和觸發器

create or replace procedure p_batchmon

astype ref_cursor_type is ref cursor;

cursor_select ref_cursor_type;

select_cname varchar2(1000);

v_file_handle utl_file.file_type;

v_sql varchar2(1000);

v_filepath varchar2(500);

v_filename varchar2(500);

v_results varchar2(500);

v_pid varchar2(1000);

v_cpcnshortname varchar2(500);

begin

v_filename:='batch_'|| substr(to_char(sysdate,'yyyymmdd'),1,10) ||'.log' ;/*寫入的檔名*/

select_cname:='select batch_id,batch_name from bfs_sys_batch_tasks where last_exec_stauts!=''00''';/*用於展示的結果集*/

v_file_handle:=utl_file.fopen('batchdir',v_filename,'w');/*開啟寫入檔案。引數 w 表示覆蓋,o表示續寫*/

open cursor_select for select_cname;

fetch cursor_select into v_pid,v_cpcnshortname;

while cursor_select%found

loop

v_results := v_pid||'|'||v_cpcnshortname||'batcherror'||'\n';

utl_file.put_line(v_file_handle,v_results);

fetch cursor_select into v_pid,v_cpcnshortname;

end loop;

close cursor_select;

utl_file.fclose(v_file_handle);

end p_batchmon;

/create or replace trigger t_batchmon

after update on  bfs_sys_batch_tasks

begin

p_batchmon;

end;

/

oracle儲存過程和觸發器

過程 建立過程,create procedure語句可以用於建立過程,簡化語法如下 create or replace procedure procedure name parameter name in out in out type begin procedure body end parame...

oracle觸發器和儲存過程

一 觸發器分為行級觸發和語句級觸發器,for each now 行級觸發起 基本語法為 create or replace trigger tri name before insert on table name for each now begin select sysdate into new....

儲存過程和觸發器 啟用和禁用觸發器

有的情況下可能需要臨時禁用觸發器,比如他引用的資料庫物件已經失效,或者需要執行大量的資料操作 此時不希望觸發器工作,以避免造成延時等等 alter trigger disable 禁用某個觸發器 alter trigger enable 啟用某個觸發器 alter table disable all...