Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, hari.

Asked: February 20, 2017 - 10:44 am UTC

Last updated: February 22, 2017 - 1:12 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi All,
i have a scenario where I want to create a trigger which will generate a flat file whenever a set of data like department number's(10,20,30,40,50...) changes(insert/updates) on a particular date(sysdate).

and Connor said...

You dont want to do it directly from a trigger, because someone might roll back the transaction - but you can't roll back a flat file write.

So you would do it like this:

a) trigger submits a job via dbms_job
b) commit will activate the job
c) job will perform the required steps, eg utl_file.

SQL> create table t ( deptno int );

Table created.

SQL>
SQL> create or replace
  2  procedure unload_Data is
  3    l_file         utl_file.file_type;
  4    l_dir          varchar2(100) := 'TEMP';
  5    l_filename     varchar2(100) := 'myfile.dat';
  6  begin
  7    l_file := utl_file.fopen(l_dir, l_filename, 'w');
  8    utl_file.put_line(l_file, 'Here is some data');
  9    utl_file.fclose(l_file);
 10  end;
 11  /

Procedure created.

SQL>
SQL> create or replace
  2  trigger t_trg
  3  after insert on t
  4  for each row
  5  declare
  6    j int;
  7  begin
  8    dbms_job.submit(j, 'unload_data;');
  9  end;
 10  /

Trigger created.

SQL>
SQL> insert into t values (10);

1 row created.

SQL> commit;

Commit complete.


SQL> host dir c:\temp\myfile.dat
 Volume in drive C is System
 Volume Serial Number is 7AD7-C05A

 Directory of c:\temp

20/02/2017  09:31 PM                19 myfile.dat
               1 File(s)             19 bytes
               0 Dir(s)  45,458,391,040 bytes free

SQL>


Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

question

A reader, February 20, 2017 - 2:48 pm UTC

how do i change the owner of the file on TEMP?
Connor McDonald
February 21, 2017 - 1:30 am UTC

You cannot (not with UTL_FILE), because changing ownership typically means 'root' level privs or copying as the target owner, and of course, the database only has one owner account.

You could consider OS level tweaking to set a default permission for files created.

Still issue

A reader, February 21, 2017 - 4:50 am UTC

I've created my directory on my fs as developer and do chmod 777 on it. File is generated as Oracle owner. So I haven't access on it.and do not have oracle pwd account. What is the solution?
Connor McDonald
February 22, 2017 - 1:05 am UTC

Try adding the sticky bit on the directory to pick up the directory permissions.

https://en.wikipedia.org/wiki/Setuid

Or something like

https://en.wikipedia.org/wiki/Inotify

to handle it outside the database. Or use the scheduler to run a script to set the permissions once the file is written.

hari A, February 21, 2017 - 6:27 am UTC

Thanks Connor for the solution code.one more query..IS there any way we can move the file to other server location?
thanks very much!!
Connor McDonald
February 22, 2017 - 1:12 am UTC

You could use dbms_file_transfer if you want the entire process controlled from within the database.

Alternatively a scheduler programming that runs an OS script could do it. Or you could use something like rsync to do it in the background via the OS

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library