Oracle writes files to disks through triggers and stored procedures

Source: Internet
Author: User

A project needs to monitor data changes in a table, and then generate a file to the disk based on the change results to facilitate monitoring by log capturing programs.

You need to use the util_file package that comes with Oracle.

The target database user is Ceps.

Use sysdba to log on and grant permissions to users:
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;


Next is the process and trigger to be used.

Create or replace procedure p_batchmon
As
Type 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'),) |'. log';/* Name of the written file */
Select_cname: = 'select batch_id, batch_name from bfs_sys_batch_tasks where last_exec_stauts! = '00';/* result set for display */
V_file_handle: = utl_file.fopen ('batchdir', v_filename, 'w');/* Open the Write File. The W parameter indicates overwrite, and the O parameter indicates continued write */
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;
/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.