Home » SQL & PL/SQL » SQL & PL/SQL » Utl_package (Oracle 10g)
Utl_package [message #579009] |
Thu, 07 March 2013 00:52  |
 |
vino06cse57
Messages: 131 Registered: July 2011 Location: chennai
|
Senior Member |
|
|
Hi All
I need a tutorial for using utl_file package to read and load in to oracle data.
Oracle is in Linux box.Please help me to proceed the initail step with no validation.
ThanKs
|
|
|
|
|
|
|
Re: Utl_package [message #579026 is a reply to message #579020] |
Thu, 07 March 2013 01:35   |
 |
Michel Cadot
Messages: 68767 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
And there are many examples here, why don't search for them?
In addition, it should far better and faster to use SQL*Loader or an external table for this.
Please read How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" or "Preview Quick Reply" button to verify.
Also always post your Oracle version, with 4 decimals.
Regards
Michel
[Updated on: Thu, 07 March 2013 01:36] Report message to a moderator
|
|
|
|
|
|
Re: Utl_package [message #579039 is a reply to message #579037] |
Thu, 07 March 2013 02:40   |
 |
vino06cse57
Messages: 131 Registered: July 2011 Location: chennai
|
Senior Member |
|
|
PLs help me in making this procedure simple to read the record
CREATE TABLE TEMP
(
COL1 NUMBER,
COL2 VARCHAR2(40 BYTE),
COL3 DATE
)
/
Data
COL1 COL2 COL3
1,AA,1-Jan-13
2,BB,1-Jan-13
3,CC,1-Jan-13
CREATE OR REPLACE PROCEDURE load_csv (
p_filename IN VARCHAR2,
p_directory IN VARCHAR2 DEFAULT 'ORA_DIR',
p_ignore_headerlines IN INTEGER DEFAULT 1,
p_delimiter IN VARCHAR2 DEFAULT ',')
IS
v_filehandle UTL_FILE.file_type;
v_text VARCHAR2 (32767);
v_eof BOOLEAN := FALSE;
v_fields DBMS_SQL.varchar2a;
v_field_index INTEGER;
v_length INTEGER;
v_start INTEGER;
v_index INTEGER;
v_enclosed_start INTEGER;
v_enclosed_end INTEGER;
BEGIN
v_filehandle := UTL_FILE.fopen (p_directory,p_filename,'r',32767);
IF p_ignore_headerlines > 0
THEN
BEGIN
FOR i IN 1 .. p_ignore_headerlines
LOOP
UTL_FILE.get_line (v_filehandle, v_text);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
v_eof := TRUE;
END;
END IF;
WHILE NOT v_eof
LOOP
BEGIN
UTL_FILE.get_line (v_filehandle, v_text);
v_fields.delete;
v_field_index := 0;
v_length := LENGTH (v_text);
v_start := 1;
v_enclosed_start := INSTR (v_text, '"', 1);
v_enclosed_end := INSTR (v_text, '"', v_enclosed_start + 1);
WHILE (v_start <= v_length)
LOOP
v_index := INSTR (v_text, p_delimiter, v_start);
IF v_enclosed_end != 0
AND v_index > v_enclosed_start
AND v_index < v_enclosed_end
THEN
v_index := INSTR (v_text, p_delimiter, v_enclosed_end);
v_enclosed_start := INSTR (v_text, '"', v_enclosed_end + 1);
IF v_enclosed_start != 0
THEN
v_enclosed_end := INSTR (v_text, '"', v_enclosed_start + 1);
END IF;
END IF;
IF v_index = 0
THEN
v_fields (v_field_index) :=
TRIM (LTRIM (RTRIM (SUBSTR (v_text, v_start), '"'), '"'));
v_start := v_length + LENGTH (p_delimiter);
ELSE
v_fields (v_field_index) :=
TRIM (
LTRIM (
RTRIM (SUBSTR (v_text, v_start, v_index - v_start),
'"'),
'"'));
v_start := v_index + LENGTH (p_delimiter);
END IF;
v_field_index := v_field_index + 1;
END LOOP;
INSERT INTO temp (col1, col2, col3)
VALUES (
v_fields (0),
v_fields (1),
TO_DATE (v_fields (3), 'MM/DD/YYYY'));
Commit;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
v_eof := TRUE;
WHEN OTHERS
THEN
DBMS_OUTPUT.
put_line ('Stop' );
END;
END LOOP;
UTL_FILE.fclose (v_filehandle);
END load_csv;
error
ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 475
ORA-29283: invalid file operation
ORA-06512: at "DASHBOARD75.LOAD_CSV", line 18
ORA-06512: at line 11
|
|
|
Re: Utl_package [message #579042 is a reply to message #579039] |
Thu, 07 March 2013 03:10   |
John Watson
Messages: 8988 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Your error is at line 18, which is your call to utl_file.fopen. So it would seem likely that you are passing rubbish as the arguments to that call.
But there are other problems pending: for example, your WHEN OTHERS clause will hide problems, your TO_DATE format doesn't match your source data.
|
|
|
|
|
|
|
Re: Utl_package [message #579056 is a reply to message #579050] |
Thu, 07 March 2013 03:41   |
John Watson
Messages: 8988 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Quote:John
Have u tested with same code in your side? Come on, man. Of course I haven't tested it. How can I, when you haven't provided the CREATE DIRECTORY statement? And if you do provide it, how can I know if it matches your reality?
(And lastly, I am not inclined to invest more time when you haven't said "thank you". Or fixed the other bugs I pointed out.)
|
|
|
|
|
Re: Utl_package [message #579059 is a reply to message #579058] |
Thu, 07 March 2013 04:00   |
 |
vino06cse57
Messages: 131 Registered: July 2011 Location: chennai
|
Senior Member |
|
|
i am doing execution in toad
CREATE OR REPLACE PROCEDURE load_csv (
p_filename IN VARCHAR2,
p_directory IN VARCHAR2 DEFAULT 'DUMPDIR1',
p_ignore_headerlines IN INTEGER DEFAULT 1,
p_delimiter IN VARCHAR2 DEFAULT ',')
IS
v_filehandle UTL_FILE.file_type;
v_text VARCHAR2 (32767);
v_eof BOOLEAN := FALSE;
v_fields DBMS_SQL.varchar2a;
v_field_index INTEGER;
v_length INTEGER;
v_start INTEGER;
v_index INTEGER;
v_enclosed_start INTEGER;
v_enclosed_end INTEGER;
BEGIN
v_filehandle := UTL_FILE.FOPEN(p_directory,p_filename,'R',32767);
IF 1 > 0
THEN
BEGIN
FOR i IN 1 .. p_ignore_headerlines
LOOP
UTL_FILE.get_line (v_filehandle, v_text);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
v_eof := TRUE;
END;
END IF;
WHILE NOT v_eof
LOOP
BEGIN
UTL_FILE.get_line (v_filehandle, v_text);
v_fields.delete;
v_field_index := 0;
v_length := LENGTH (v_text);
v_start := 1;
v_enclosed_start := INSTR (v_text, '"', 1);
v_enclosed_end := INSTR (v_text, '"', v_enclosed_start + 1);
WHILE (v_start <= v_length)
LOOP
v_index := INSTR (v_text, p_delimiter, v_start);
IF v_enclosed_end != 0
AND v_index > v_enclosed_start
AND v_index < v_enclosed_end
THEN
v_index := INSTR (v_text, p_delimiter, v_enclosed_end);
v_enclosed_start := INSTR (v_text, '"', v_enclosed_end + 1);
IF v_enclosed_start != 0
THEN
v_enclosed_end := INSTR (v_text, '"', v_enclosed_start + 1);
END IF;
END IF;
IF v_index = 0
THEN
v_fields (v_field_index) :=
TRIM (LTRIM (RTRIM (SUBSTR (v_text, v_start), '"'), '"'));
v_start := v_length + LENGTH (p_delimiter);
ELSE
v_fields (v_field_index) :=
TRIM (
LTRIM (
RTRIM (SUBSTR (v_text, v_start, v_index - v_start),
'"'),
'"'));
v_start := v_index + LENGTH (p_delimiter);
END IF;
v_field_index := v_field_index + 1;
END LOOP;
INSERT INTO temp (col1, col2, col3)
VALUES (
v_fields (0),
v_fields (1),
TO_DATE (v_fields (3),'DD-Mon-YY'));
Commit;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
v_eof := TRUE;
END;
END LOOP;
UTL_FILE.fclose (v_filehandle);
END;
DECLARE
P_FILENAME VARCHAR2(200):= 'DATA_PUMP_07_Mar_13.csv';
P_DIRECTORY VARCHAR2(200):= 'DUMPDIR1';
P_IGNORE_HEADERLINES NUMBER := 1;
BEGIN
LOAD_CSV( P_FILENAME, P_DIRECTORY, P_IGNORE_HEADERLINES);
END;
[Updated on: Thu, 07 March 2013 04:02] Report message to a moderator
|
|
|
|
|
|
Re: Utl_package [message #579067 is a reply to message #579009] |
Thu, 07 March 2013 06:53   |
Solomon Yakobson
Messages: 3305 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
vino06cse57 wrote on Thu, 07 March 2013 01:52Oracle is in Linux box.
You are getting error trying to open the file. A sanity check:
UTL_FILE is server side PL/SQL package, therefore it can only deal with directories and files that reside on volumes mounted on database server. You can't reference client side directories in UTL_FILE. So make sure Oracle directory object DUMPDIR1 points to database server directory. Make sure file DATA_PUMP_07_Mar_13.csv exists in that server directory and OS user oracle (user under which Oracle software was installed, to be precise) has read permission on the file.
SY.
|
|
|
Re: Utl_package [message #579069 is a reply to message #579064] |
Thu, 07 March 2013 06:56   |
 |
vino06cse57
Messages: 131 Registered: July 2011 Location: chennai
|
Senior Member |
|
|
SQL> CREATE OR REPLACE PROCEDURE load_csv (
2 p_filename IN VARCHAR2,
3 p_directory IN VARCHAR2 DEFAULT 'DUMPDIR1',
4 p_ignore_headerlines IN INTEGER DEFAULT 1,
5 p_delimiter IN VARCHAR2 DEFAULT ',')
6 IS
7 /*******************************************************************
8 -- PROCEDURE LOAD_CSV
9 -- PURPOSE: This Procedure read the data from a CSV file.
10 -- And load it into the target oracle table.
11 -- Finally it renames the source file with date.
12 --
13 -- P_FILENAME
14 -- The name of the flat file(a text file)
15 --
16 -- P_DIRECTORY
17 -- Name of the directory where the file is been placed.
18 -- Note: The grant has to be given for the user to the directory
19 -- before executing the function
20 --
21 -- P_IGNORE_HEADERLINES:
22 -- Pass the value as '1' to ignore importing headers.
23 --
24 -- P_DELIMITER
25 -- By default the delimiter is used as ','
26 -- As we are using CSV file to load the data into oracle
27 --
28 -- AUTHOR:
29 -- Sloba
30 -- Version 1.0
31 ********************************************************************
32 v_filehandle UTL_FILE.file_type;
33 v_text VARCHAR2(32767);
34 v_eof BOOLEAN := FALSE;
35 v_fields DBMS_SQL.varchar2a;
36 v_field_index INTEGER;
37 v_length INTEGER;
38 v_start INTEGER;
39 v_index INTEGER;
40 v_enclosed_start INTEGER;
41 v_enclosed_end INTEGER;
42 BEGIN
43 v_filehandle := UTL_FILE.fopen(p_directory, p_filename, 'r', 32767)
44
45 IF p_ignore_headerlines > 0
46 THEN
47 BEGIN
48 FOR i IN 1 .. p_ignore_headerlines
49 LOOP
50 UTL_FILE.get_line(v_filehandle, v_text);
51 END LOOP;
52 EXCEPTION
53 WHEN NO_DATA_FOUND
54 THEN
55 v_eof := TRUE;
56 END;
57 END IF;
58
59 WHILE NOT v_eof
60 LOOP
61 BEGIN
62 DBMS_OUTPUT.put_line(
63 '------------------------------------------------------------------
64 UTL_FILE.get_line(v_filehandle, v_text);
65 DBMS_OUTPUT.put_line('v_text=' || v_text);
66 v_fields.delete;
67 v_field_index := 0;
68 v_length := LENGTH(v_text);
69 v_start := 1;
70 v_enclosed_start := INSTR(v_text, '"', 1);
71 v_enclosed_end := INSTR(v_text, '"', v_enclosed_start + 1);
72
73 WHILE (v_start <= v_length)
74 LOOP
75 v_index := INSTR(v_text, p_delimiter, v_start);
76
77 IF v_enclosed_end != 0
78 AND v_index > v_enclosed_start
79 AND v_index < v_enclosed_end
80 THEN
81 v_index := INSTR(v_text, p_delimiter, v_enclosed_end);
82 v_enclosed_start := INSTR(v_text, '"', v_enclosed_end + 1);
83
84 IF v_enclosed_start != 0
85 THEN
86 v_enclosed_end :=
87 INSTR(v_text, '"', v_enclosed_start + 1);
88 END IF;
89 END IF;
90
91 IF v_index = 0
92 THEN
93 v_fields(v_field_index) :=
94 TRIM(LTRIM(RTRIM(SUBSTR(v_text, v_start), '"'), '"'));
95 v_start := v_length + LENGTH(p_delimiter);
96 ELSE
97 v_fields(v_field_index) :=
98 TRIM(
99 LTRIM(
100 RTRIM(
101 SUBSTR(v_text, v_start, v_index - v_start),
102 '"'),
103 '"'));
104 v_start := v_index + LENGTH(p_delimiter);
105 END IF;
106
107 v_field_index := v_field_index + 1;
108 END LOOP;
109 INSERT
110 INTO temp(col1,col2, col3)
111 VALUES (
112 v_fields(0),
113 v_fields(1),
114 TO_DATE(v_fields(2), 'DD-Mon-YY'));
115 commit;
116 DBMS_OUTPUT.put_line(
117 '------------------------------------------------------------------
118 EXCEPTION
119 WHEN NO_DATA_FOUND
120 THEN
121 v_eof := TRUE;
122 WHEN OTHERS
123 THEN
124 DBMS_OUTPUT.put_line(
125 '*************************************************************');
126 DBMS_OUTPUT.put_line(SQLERRM);
127 DBMS_OUTPUT.put_line(v_text);
128 DBMS_OUTPUT.put_line(
129 '*************************************************************');
130 END;
131 END LOOP;
132
133 UTL_FILE.fclose(v_filehandle);
134
135 EXCEPTION
136 WHEN OTHERS
137 THEN
138 IF UTL_FILE.is_open(v_filehandle)
139 THEN
140 UTL_FILE.fclose(v_filehandle);
141 END IF;
142
143 RAISE;
144 END load_csv;
145
146 /
Procedure created.
SQL>
Wrote file afiedt.buf
1 DECLARE
2 P_FILENAME VARCHAR2(200):= 'tmp.csv';
3 P_DIRECTORY VARCHAR2(200):= 'DUMPDIR1';
4 P_IGNORE_HEADERLINES NUMBER := 1;
5 BEGIN
6 LOAD_CSV( P_FILENAME, P_DIRECTORY, P_IGNORE_HEADERLINES);
7* END ;
8 /
PL/SQL procedure successfully completed.
SQL>
|
|
|
Re: Utl_package [message #579070 is a reply to message #579069] |
Thu, 07 March 2013 07:05   |
John Watson
Messages: 8988 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Your source data uses a space as a delimiter, but you are telling your procedure that a comma is the delimiter. And you still haven't fixed your date format.
--
update: oh, the date format has been fixed. Edits to posts are confusing sometimes.
[Updated on: Thu, 07 March 2013 07:11] Report message to a moderator
|
|
|
Re: Utl_package [message #579071 is a reply to message #579069] |
Thu, 07 March 2013 07:06   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
vino06cse57 wrote on Thu, 07 March 2013 12:56135 EXCEPTION
136 WHEN OTHERS
137 THEN
138 IF UTL_FILE.is_open(v_filehandle)
139 THEN
140 UTL_FILE.fclose(v_filehandle);
141 END IF;
142
143 RAISE;
144 END load_csv;
145
146 /
We told you to get rid of the exception when others didn't we?
And the post above which shows the code you said you were running in toad doesn't have them.
So what you said you were running isn't what you are actually running.
Get rid of all the exception when others and try again.
EDIT: missing word
[Updated on: Thu, 07 March 2013 07:06] Report message to a moderator
|
|
|
Re: Utl_package [message #579073 is a reply to message #579071] |
Thu, 07 March 2013 07:21   |
 |
vino06cse57
Messages: 131 Registered: July 2011 Location: chennai
|
Senior Member |
|
|
I tried by removing the other exception
SQL> CREATE OR REPLACE PROCEDURE load_csv (
2 p_filename IN VARCHAR2,
3 p_directory IN VARCHAR2 DEFAULT 'DUMPDIR1',
4 p_ignore_headerlines IN INTEGER DEFAULT 1,
5 p_delimiter IN VARCHAR2 DEFAULT ',')
6 IS
7
8 v_filehandle UTL_FILE.file_type;
9 v_text VARCHAR2(32767);
10 v_eof BOOLEAN := FALSE;
11 v_fields DBMS_SQL.varchar2a;
12 v_field_index INTEGER;
13 v_length INTEGER;
14 v_start INTEGER;
15 v_index INTEGER;
16 v_enclosed_start INTEGER;
17 v_enclosed_end INTEGER;
18 BEGIN
19 v_filehandle := UTL_FILE.fopen(p_directory, p_filename, 'r', 3276
20
21 IF p_ignore_headerlines > 0
22 THEN
23 BEGIN
24 FOR i IN 1 .. p_ignore_headerlines
25 LOOP
26 UTL_FILE.get_line(v_filehandle, v_text);
27 END LOOP;
28 EXCEPTION
29 WHEN NO_DATA_FOUND
30 THEN
31 v_eof := TRUE;
32 END;
33 END IF;
34
35 WHILE NOT v_eof
36 LOOP
37 BEGIN
38 DBMS_OUTPUT.put_line(
39 '----------------------------------------------------------------
40 UTL_FILE.get_line(v_filehandle, v_text);
41 DBMS_OUTPUT.put_line('v_text=' || v_text);
42 v_fields.delete;
43 v_field_index := 0;
44 v_length := LENGTH(v_text);
45 v_start := 1;
46 v_enclosed_start := INSTR(v_text, '"', 1);
47 v_enclosed_end := INSTR(v_text, '"', v_enclosed_start + 1);
48
49 WHILE (v_start <= v_length)
50 LOOP
51 v_index := INSTR(v_text, p_delimiter, v_start);
52
53 IF v_enclosed_end != 0
54 AND v_index > v_enclosed_start
55 AND v_index < v_enclosed_end
56 THEN
57 v_index := INSTR(v_text, p_delimiter, v_enclosed_end);
58 v_enclosed_start := INSTR(v_text, '"', v_enclosed_end + 1);
59
60 IF v_enclosed_start != 0
61 THEN
62 v_enclosed_end :=
63 INSTR(v_text, '"', v_enclosed_start + 1);
64 END IF;
65 END IF;
66
67 IF v_index = 0
68 THEN
69 v_fields(v_field_index) :=
70 TRIM(LTRIM(RTRIM(SUBSTR(v_text, v_start), '"'), '"'));
71 v_start := v_length + LENGTH(p_delimiter);
72 ELSE
73 v_fields(v_field_index) :=
74 TRIM(
75 LTRIM(
76 RTRIM(
77 SUBSTR(v_text, v_start, v_index - v_start),
78 '"'),
79 '"'));
80 v_start := v_index + LENGTH(p_delimiter);
81 END IF;
82
83 v_field_index := v_field_index + 1;
84 END LOOP;
85 INSERT
86 INTO temp(col1,col2, col3)
87 VALUES (
88 v_fields(0),
89 v_fields(1),
90 TO_DATE(v_fields(2), 'DD-Mon-YY'));
91 commit;
92
93 EXCEPTION
94 WHEN NO_DATA_FOUND
95 THEN
96 v_eof := TRUE;
97
98 END;
99 END LOOP;
100
101 UTL_FILE.fclose(v_filehandle);
102 END load_csv;
103
104 /
Procedure created.
SQL> DECLARE
2
3 P_FILENAME VARCHAR2(200):= 'tmp.csv';
4
5 P_DIRECTORY VARCHAR2(200):= 'DUMPDIR1';
6
7 P_IGNORE_HEADERLINES NUMBER := 1;
8
9 BEGIN
10
11 LOAD_CSV( P_FILENAME, P_DIRECTORY, P_IGNORE_HEADERLINES);
12 END ;
13 /
PL/SQL procedure successfully completed.
SQL> select * from temp;
no rows selected
SQL>
|
|
|
|
|
Re: Utl_package [message #579076 is a reply to message #579073] |
Thu, 07 March 2013 07:25   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Have you got serveroutput on?
If not, switch it on and try again.
If it is on then the lack of output indicates that the file is empty.
|
|
|
|
|
|
Goto Forum:
Current Time: Tue Aug 26 19:21:58 CDT 2025
|