Home » SQL & PL/SQL » SQL & PL/SQL » Use of UTL_FILE
Use of UTL_FILE [message #6767] Mon, 05 May 2003 22:07 Go to next message
LASCOLI
Messages: 18
Registered: November 2002
Junior Member
I am trying to dump a .csv file with all of whitespaces removed from the query listed below. I was attempting to use UTL_FILE function, but I am not having much success as I have virtually no PLSQL experience. Any information would be most welcome. Thank you.

I am using 8i ,I believe I set the initSID.ora correctly (utl_file_dir = /home/oracle).

1st Error when attempting to grant write access to user. Is tis necessary for 8i since ?

SQL> GRANT WRITE ON DIRECTORY EXTRACT_DIR TO dbimpl
2 ;
GRANT WRITE ON DIRECTORY EXTRACT_DIR TO dbimpl
*
ERROR at line 1:
ORA-22928: invalid privilege on directories

Attempt at PLSQL

CREATE OR REPLACE PROCEDURE CRTREP_CSV AS
CURSOR c_data IS
select r.DESTINATION_NATIONAL_ID "Code",r.CALL_PROCESSING_ELEMENT1_ID "ENT_ID", r.Routing_label_id "Routing_Label", r.Partition_id "Code_Par","1stRTE_A","1stRTE_Z", "1stRTE_%", "2ndRTE_A", "2ndRTE_Z", "2ndRTE_%", "3rdRTE_A","3rdRTE_Z", "3rdRTE_%", "4thRTE_A","4thRTE_Z", "4thRTE_%", p.Partition_id "RL_Par"
from route r ,
( select rlr.routing_label_id , rl.partition_id,
max(decode(route_sequence, 1, ROUTE_ENDPOINT1, null)) "1stRTE_A",
max(decode(route_sequence, 1, ROUTE_ENDPOINT2, null)) "1stRTE_Z",
max(decode(route_sequence, 1, ROUTE_PROPORTION)) "1stRTE_%",
max(decode(route_sequence, 2, ROUTE_ENDPOINT1, null)) "2ndRTE_A",
max(decode(route_sequence, 2, ROUTE_ENDPOINT2, null)) "2ndRTE_Z",
max(decode(route_sequence, 2, ROUTE_PROPORTION)) "2ndRTE_%",
max(decode(route_sequence, 3, ROUTE_ENDPOINT1, null)) "3rdRTE_A",
max(decode(route_sequence, 3, ROUTE_ENDPOINT2, null)) "3rdRTE_Z",
max(decode(route_sequence, 3, ROUTE_PROPORTION)) "3rdRTE_%",
max(decode(route_sequence, 4, ROUTE_ENDPOINT1, null)) "4thRTE_A",
max(decode(route_sequence, 4, ROUTE_ENDPOINT2, null)) "4thRTE_Z",
max(decode(route_sequence, 4, ROUTE_PROPORTION)) "4thRTE_%"
from routing_label_routes rlr , routing_label rl
where rl.routing_label_id = rlr.routing_label_id
group by rlr. routing_label_id, rl.partition_id) p
where p.routing_label_id =r.routing_label_id(+)
UNION
select r.DESTINATION_NATIONAL_ID "Code",r.CALL_PROCESSING_ELEMENT1_ID "ENT_ID", r.Routing_label_id "Routing_Label", r.Partition_id "Code_Par","1stRTE_A","1stRTE_Z", "1stRTE_%", "2ndRTE_A", "2ndRTE_Z", "2ndRTE_%", "3rdRTE_A","3rdRTE_Z", "3rdRTE_%", "4thRTE_A","4thRTE_Z", "4thRTE_%", p.Partition_id "RL_Par"
from route r ,
( select rlr.routing_label_id , rl.partition_id,
max(decode(route_sequence, 1, ROUTE_ENDPOINT1, null)) "1stRTE_A",
max(decode(route_sequence, 1, ROUTE_ENDPOINT2, null)) "1stRTE_Z",
max(decode(route_sequence, 1, ROUTE_PROPORTION)) "1stRTE_%",
max(decode(route_sequence, 2, ROUTE_ENDPOINT1, null)) "2ndRTE_A",
max(decode(route_sequence, 2, ROUTE_ENDPOINT2, null)) "2ndRTE_Z",
max(decode(route_sequence, 2, ROUTE_PROPORTION)) "2ndRTE_%",
max(decode(route_sequence, 3, ROUTE_ENDPOINT1, null)) "3rdRTE_A",
max(decode(route_sequence, 3, ROUTE_ENDPOINT2, null)) "3rdRTE_Z",
max(decode(route_sequence, 3, ROUTE_PROPORTION)) "3rdRTE_%",
max(decode(route_sequence, 4, ROUTE_ENDPOINT1, null)) "4thRTE_A",
max(decode(route_sequence, 4, ROUTE_ENDPOINT2, null)) "4thRTE_Z",
max(decode(route_sequence, 4, ROUTE_PROPORTION)) "4thRTE_%"
from routing_label_routes rlr , routing_label rl
where rl.routing_label_id = rlr.routing_label_id
group by rlr. routing_label_id, rl.partition_id) p
where p.routing_label_id(+) =r.routing_label_id
;

v_file UTL_FILE.FILE_TYPE;
BEGIN
v_file := UTL_FILE.FOPEN(location => '/home/oracle',
filename => 'crtrep_csv.txt',
open_mode => 'w',
max_linesize => 32767);
FOR cur_rec IN c_data LOOP
UTL_FILE.PUT_LINE(v_file,
cur_rec.Code || ',' ||
cur_rec.ENT_ID || ',' ||
cur_rec.Routing_Label || ',' ||
cur_rec.Code_Par || ',' ||
cur_rec.1stRTE_A || ',' ||
cur_rec.1stRTE_Z || ',' ||
cur_rec.1stRTE_%", || ',' ||
cur_rec.2ndRTE_A || ',' ||
cur_rec.2ndRTE_Z || ',' ||
cur_rec.2ndRTE_%", || ',' ||
cur_rec.3rdRTE_A || ',' ||
cur_rec.3rdRTE_Z || ',' ||
cur_rec.3rdRTE_%", || ',' ||
cur_rec.4thRTE_A || ',' ||
cur_rec.4thRTE_Z || ',' ||
cur_rec.4thRTE_%");
END LOOP;
UTL_FILE.FCLOSE(v_file);

EXCEPTION
WHEN OTHERS THEN
UTL_FILE.FCLOSE(v_file);
END;
/

2nd Error
SQL> @/home/oracle/CRTREP_CSV.txt

Warning: Procedure created with compilation errors.

3rd Error

SQL> @/home/oracle/CRTREP_CSV.txt

Warning: Procedure created with compilation errors.

SQL> exec CRTREP_CSV.txt
BEGIN CRTREP_CSV.txt; END;

*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00905: object DBIMPL.CRTREP_CSV is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Re: Use of UTL_FILE [message #6770 is a reply to message #6767] Tue, 06 May 2003 00:02 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
Please click on the link below for information on setting the utl_file_dir parameter in your init.ora file, after which you must bounce the database for it to take effect. This provides read/write access to all all files within this directory to all users. The directory must be a server directory. Utl_file does not work on client directories.



Please click on the link below for a nice generic utility by Tom Kyte, which accepts any query and any delimiter as parameters, and outputs the result to a comma-separated-value file:



So, all you need to do is set up your utl_file_dir parameter in your init.ora file, bounce the database, run Tom's script to create the dump_csv function, then simplify your procedure so that it is like the test_dump_csv procedure in Tom's example, substituting your query and directory for his. If you have any further problems please post a cut and paste of the code you ran and any error messages received.
Re: Use of UTL_FILE [message #6796 is a reply to message #6770] Tue, 06 May 2003 12:56 Go to previous messageGo to next message
LASCOLI
Messages: 18
Registered: November 2002
Junior Member
I continue to receive compilation errors when I attempt to create the procedure ? Thanks again

SQL> create or replace procedure test_dump_csv
2 as
3 l_rows number;
4 begin l_rows := dump_csv (select r.DESTINATION_NATIONAL_ID "Code",r.CALL_PROCESSING_ELEMENT1_ID "ENT_ID", r.Routing_label_id "Routing_Label", r.Partition_id "Code_Par","1stRTE_A","1stRTE_Z", "1stRTE_%", "2ndRTE_A", "2ndRTE_Z", "2ndRTE_%", "3rdRTE_A","3rdRTE_Z", "3rdRTE_%", "4thRTE_A","4thRTE_Z", "4thRTE_%", p.Partition_id "RL_Par"
5 from route r ,
6 ( select rlr.routing_label_id , rl.partition_id,
7 max(decode(route_sequence, 1, ROUTE_ENDPOINT1, null)) "1stRTE_A",
8 max(decode(route_sequence, 1, ROUTE_ENDPOINT2, null)) "1stRTE_Z",
9 max(decode(route_sequence, 1, ROUTE_PROPORTION)) "1stRTE_%",
10 max(decode(route_sequence, 2, ROUTE_ENDPOINT1, null)) "2ndRTE_A",
11 max(decode(route_sequence, 2, ROUTE_ENDPOINT2, null)) "2ndRTE_Z",
max(decode(route_sequence, 2, ROUTE_PROPORTION)) "2ndRTE_%",
12 13 max(decode(route_sequence, 3, ROUTE_ENDPOINT1, null)) "3rdRTE_A",
14 max(decode(route_sequence, 3, ROUTE_ENDPOINT2, null)) "3rdRTE_Z",
15 max(decode(route_sequence, 3, ROUTE_PROPORTION)) "3rdRTE_%",
16 max(decode(route_sequence, 4, ROUTE_ENDPOINT1, null)) "4thRTE_A",
17 max(decode(route_sequence, 4, ROUTE_ENDPOINT2, null)) "4thRTE_Z",
18 max(decode(route_sequence, 4, ROUTE_PROPORTION)) "4thRTE_%"
19 from routing_label_routes rlr , routing_label rl
20 where rl.routing_label_id = rlr.routing_label_id
21 group by rlr. routing_label_id, rl.partition_id) p
22 where p.routing_label_id =r.routing_label_id(+)
23 UNION
24 select r.DESTINATION_NATIONAL_ID "Code",r.CALL_PROCESSING_ELEMENT1_ID "ENT_ID", r.Routing_label_id "Routing_Label", r.Partition_id "Code_Par","1stRTE_A","1stRTE_Z", "1stRTE_%", "2ndRTE_A", "2ndRTE_Z", "2ndRTE_%", "3rdRTE_A","3rdRTE_Z", "3rdRTE_%", "4thRTE_A","4thRTE_Z", "4thRTE_%", p.Partition_id "RL_Par"
25 from route r ,
( select rlr.routing_label_id , rl.partition_id,
26 27 max(decode(route_sequence, 1, ROUTE_ENDPOINT1, null)) "1stRTE_A",
28 max(decode(route_sequence, 1, ROUTE_ENDPOINT2, null)) "1stRTE_Z",
29 max(decode(route_sequence, 1, ROUTE_PROPORTION)) "1stRTE_%",
30 max(decode(route_sequence, 2, ROUTE_ENDPOINT1, null)) "2ndRTE_A",
31 max(decode(route_sequence, 2, ROUTE_ENDPOINT2, null)) "2ndRTE_Z",
32 max(decode(route_sequence, 2, ROUTE_PROPORTION)) "2ndRTE_%",
33 max(decode(route_sequence, 3, ROUTE_ENDPOINT1, null)) "3rdRTE_A",
34 max(decode(route_sequence, 3, ROUTE_ENDPOINT2, null)) "3rdRTE_Z",
35 max(decode(route_sequence, 3, ROUTE_PROPORTION)) "3rdRTE_%",
36 max(decode(route_sequence, 4, ROUTE_ENDPOINT1, null)) "4thRTE_A",
37 max(decode(route_sequence, 4, ROUTE_ENDPOINT2, null)) "4thRTE_Z",
38 max(decode(route_sequence, 4, ROUTE_PROPORTION)) "4thRTE_%"
39 from routing_label_routes rlr , routing_label rl
40 where rl.routing_label_id = rlr.routing_label_id
41 group by rlr. routing_label_id, rl.partition_id) p
42 where p.routing_label_id(+) =r.routing_label_id ;)
43 end;
44 /

Warning: Procedure created with compilation errors.
Re: Use of UTL_FILE [message #6814 is a reply to message #6796] Wed, 07 May 2003 01:02 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
Your query needs to be enclosed within single quotes and you need to omit the semicolon, so that it can be passed to the dump_csv function as a varchar2 parameter. Also, you left out the rest of the parameters: the delimiter, the directory, and the filename. I have listed Tom's procedure below, formatting it so that the query that you need to replace is on one line and the other parameters are on another line and labeling those with comments. I have also listed below that an adaptation of his procedure for your situation, replacing his query and other parameters with yours. This assumes that you have set up everything correctly for utl_file and created the dump_csv function. Depending on how you type your query, you may need to skip some of the formatting in order to eliminate some spaces that lengthen the query. The maximum limit is 4000 characters. In my example below, the query was 3,161 characters. One other error that I noticed was that, in your group by clauses, you had rlr. routing_label_id, with a space after the period, instead of rlr.routing_label_id without the space. This would be sufficient to cause compilation errors. If you type SHOW ERRORS immediately after your code, it will tell you what line number the errors are on and more specific information as to the error.

-- Tom's original test_dump_csv procedure
-- with some formatting and comments:
create or replace procedure test_dump_csv
as
    l_rows  number;
begin
    l_rows := dump_csv ( 
-- replace the query on the next line with yours:    
    'select * from all_users where rownum < 25'
-- replace the delimiter, directory, and file below with yours:
    , ',', '/tmp', 'test.dat' 
    );
end;
/
--
--
-- adaptation of above procedure to your needs:
create or replace procedure test_dump_csv
as
    l_rows  number;
begin
    l_rows := dump_csv ( 
'select r.DESTINATION_NATIONAL_ID "Code",
        r.CALL_PROCESSING_ELEMENT1_ID "ENT_ID",
        r.Routing_label_id "Routing_Label",
        r.Partition_id "Code_Par",
        "1stRTE_A","1stRTE_Z","1stRTE_%",
        "2ndRTE_A","2ndRTE_Z","2ndRTE_%",
        "3rdRTE_A","3rdRTE_Z","3rdRTE_%",
        "4thRTE_A","4thRTE_Z","4thRTE_%",p.Partition_id "RL_Par"
 from   route r,
        (select rlr.routing_label_id,rl.partition_id,
                max(decode(route_sequence,1,ROUTE_ENDPOINT1,null)) "1stRTE_A",
                max(decode(route_sequence,1,ROUTE_ENDPOINT2,null)) "1stRTE_Z",
                max(decode(route_sequence,1,ROUTE_PROPORTION)) "1stRTE_%",
                max(decode(route_sequence,2,ROUTE_ENDPOINT1,null)) "2ndRTE_A",
                max(decode(route_sequence,2,ROUTE_ENDPOINT2,null)) "2ndRTE_Z",
                max(decode(route_sequence,2,ROUTE_PROPORTION)) "2ndRTE_%",
                max(decode(route_sequence,3,ROUTE_ENDPOINT1,null)) "3rdRTE_A",
                max(decode(route_sequence,3,ROUTE_ENDPOINT2,null)) "3rdRTE_Z",
                max(decode(route_sequence,3,ROUTE_PROPORTION)) "3rdRTE_%",
                max(decode(route_sequence,4,ROUTE_ENDPOINT1,null)) "4thRTE_A",
                max(decode(route_sequence,4,ROUTE_ENDPOINT2,null)) "4thRTE_Z",
                max(decode(route_sequence,4,ROUTE_PROPORTION)) "4thRTE_%"
         from   routing_label_routes rlr,routing_label rl
         where  rl.routing_label_id=rlr.routing_label_id
         group by rlr.routing_label_id,rl.partition_id) p
         where p.routing_label_id=r.routing_label_id(+)
UNION
select r.DESTINATION_NATIONAL_ID "Code",
       r.CALL_PROCESSING_ELEMENT1_ID "ENT_ID",
       r.Routing_label_id "Routing_Label",
       r.Partition_id "Code_Par",
       "1stRTE_A","1stRTE_Z","1stRTE_%",
       "2ndRTE_A","2ndRTE_Z","2ndRTE_%",
       "3rdRTE_A","3rdRTE_Z","3rdRTE_%",
       "4thRTE_A","4thRTE_Z","4thRTE_%", p.Partition_id "RL_Par"
from   route r,
       (select rlr.routing_label_id,rl.partition_id,
               max(decode(route_sequence,1,ROUTE_ENDPOINT1,null)) "1stRTE_A",
               max(decode(route_sequence,1,ROUTE_ENDPOINT2,null)) "1stRTE_Z",
               max(decode(route_sequence,1,ROUTE_PROPORTION)) "1stRTE_%",
               max(decode(route_sequence,2,ROUTE_ENDPOINT1,null)) "2ndRTE_A",
               max(decode(route_sequence,2,ROUTE_ENDPOINT2,null)) "2ndRTE_Z",
               max(decode(route_sequence,2,ROUTE_PROPORTION)) "2ndRTE_%",
               max(decode(route_sequence,3,ROUTE_ENDPOINT1,null)) "3rdRTE_A",
               max(decode(route_sequence,3,ROUTE_ENDPOINT2,null)) "3rdRTE_Z",
               max(decode(route_sequence,3,ROUTE_PROPORTION)) "3rdRTE_%",
               max(decode(route_sequence,4,ROUTE_ENDPOINT1,null)) "4thRTE_A",
               max(decode(route_sequence,4,ROUTE_ENDPOINT2,null)) "4thRTE_Z",
               max(decode(route_sequence,4,ROUTE_PROPORTION)) "4thRTE_%"
        from   routing_label_routes rlr,routing_label rl
        where  rl.routing_label_id=rlr.routing_label_id
        group by rlr.routing_label_id,rl.partition_id) p
        where p.routing_label_id(+)=r.routing_label_id'
    , ',', '/home/oracle', 'crtrep_csv.txt' 
    );
end;
/
SHOW ERRORS
Re: Use of UTL_FILE [message #6840 is a reply to message #6796] Wed, 07 May 2003 14:49 Go to previous message
LASCOLI
Messages: 18
Registered: November 2002
Junior Member
Thank you. I had to stop and start the instance to reference the UTL_FILE directory via init.ora file, and it worked ! Thank you very much !
Previous Topic: DBMS_JOB Question
Next Topic: Procedure Problem
Goto Forum:
  


Current Time: Tue Apr 23 22:36:13 CDT 2024