Home » SQL & PL/SQL » SQL & PL/SQL » Utl_package (Oracle 10g)
Utl_package [message #579009] Thu, 07 March 2013 00:52 Go to next message
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 #579011 is a reply to message #579009] Thu, 07 March 2013 00:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
You can read the documentation.
You can search here for UTL_FILE there are many examples.
You can search on Google too.

Regards
Michel
Re: Utl_package [message #579013 is a reply to message #579011] Thu, 07 March 2013 01:08 Go to previous messageGo to next message
vino06cse57
Messages: 131
Registered: July 2011
Location: chennai
Senior Member
Hi Michel

I had tried.But i didnt have any result for using Linux box as a server.pls help

Thanks
Re: Utl_package [message #579017 is a reply to message #579013] Thu, 07 March 2013 01:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Try again.
I don't understand what you don't understand using this package which one the most simple one.

Regards
Michel
Re: Utl_package [message #579020 is a reply to message #579017] Thu, 07 March 2013 01:28 Go to previous messageGo to next message
vino06cse57
Messages: 131
Registered: July 2011
Location: chennai
Senior Member
Michel


I need a simple example for using utl_file to load data to a oracle table,where as the server is oracle box...

just help me by giving a procedure to read a file for the below test data.where as my data file will reside in linux machine

Data

COL1 COL2 COL3
1 AA 01-Jan-13
2 BB 01-Jan-13
3 CC 01-Jan-13


Into table

CREATE TABLE TEMP
(
COL1 NUMBER,
COL2 VARCHAR2(40 BYTE),
COL3 DATE
)
/
Re: Utl_package [message #579026 is a reply to message #579020] Thu, 07 March 2013 01:35 Go to previous messageGo to next message
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 #579028 is a reply to message #579026] Thu, 07 March 2013 01:42 Go to previous messageGo to next message
vino06cse57
Messages: 131
Registered: July 2011
Location: chennai
Senior Member
Michel

SQL Loader is not recommeded.Because the PLSQl will be scheduled by third partyschedulaer.


Please help me in by giveng a procedure to read and load into table..

i had tried but it seems confusing..pls help me in this

Thanks
Re: Utl_package [message #579031 is a reply to message #579028] Thu, 07 March 2013 01:50 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
vino06cse57
i had tried but it seems confusing

Yes, I believe it does. But don't worry, it just means that you need to spend some more time in studying the subject.
Re: Utl_package [message #579037 is a reply to message #579031] Thu, 07 March 2013 02:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
... and post what you tried.

Regards
Michel
Re: Utl_package [message #579039 is a reply to message #579037] Thu, 07 March 2013 02:40 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 #579046 is a reply to message #579042] Thu, 07 March 2013 03:17 Go to previous messageGo to next message
vino06cse57
Messages: 131
Registered: July 2011
Location: chennai
Senior Member
john

 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; 

to execute
Re: Utl_package [message #579047 is a reply to message #579046] Thu, 07 March 2013 03:21 Go to previous messageGo to next message
John Watson
Messages: 8988
Registered: January 2010
Location: Global Village
Senior Member
I have no idea if those parameter values are valid. You think they are, but Oracle does not.
Re: Utl_package [message #579050 is a reply to message #579047] Thu, 07 March 2013 03:27 Go to previous messageGo to next message
vino06cse57
Messages: 131
Registered: July 2011
Location: chennai
Senior Member
John

Have u tested with same code in your side?
Re: Utl_package [message #579051 is a reply to message #579050] Thu, 07 March 2013 03:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
First remove:
Quote:
        WHEN OTHERS
         THEN
            DBMS_OUTPUT.
             put_line ('Stop' );

And retry.

We cannot try your code in our environment as we have not yours.
Does directory pointed by DUMPDIR1 exist and is accessible in read mode?
Does DATA_PUMP_07_Mar_13.csv exist in this directory and is readable?

Regards
Michel
Re: Utl_package [message #579056 is a reply to message #579050] Thu, 07 March 2013 03:41 Go to previous messageGo to next message
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 #579057 is a reply to message #579051] Thu, 07 March 2013 03:56 Go to previous messageGo to next message
vino06cse57
Messages: 131
Registered: July 2011
Location: chennai
Senior Member
Michel

I removed the exception...

Now the plsql is executing without any error..But no data is loaded into the table.

thakns
Re: Utl_package [message #579058 is a reply to message #579057] Thu, 07 March 2013 03:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Post the code.
Post the answers to our question.
Copy and paste the execution and what you did to check what you said.
Note: all must be done in the same SQL*Plus session.

Regards
Michel
Re: Utl_package [message #579059 is a reply to message #579058] Thu, 07 March 2013 04:00 Go to previous messageGo to next message
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 #579060 is a reply to message #579059] Thu, 07 March 2013 04:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Use SQL*Plus so we can see the execution.
And FIRST answers ALL our questions.

Regards
Michel
Re: Utl_package [message #579061 is a reply to message #579060] Thu, 07 March 2013 04:56 Go to previous messageGo to next message
vino06cse57
Messages: 131
Registered: July 2011
Location: chennai
Senior Member
Micheal

I used the sql*plus.

When i execute,it shows PL/SQL procedure successfully completed.

But no record inserted into the table

Answer for your question

1.Yes it had all permissions
2.the file is available in the directory mentioned
Re: Utl_package [message #579064 is a reply to message #579061] Thu, 07 March 2013 05:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
I don't believe, I think you lie.
Copy and paste your SQL*Plus session.
Prove your claims about the questions, it is not just say, yes, no, it is to prove it.

Regards
Michel
Re: Utl_package [message #579067 is a reply to message #579009] Thu, 07 March 2013 06:53 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
vino06cse57 wrote on Thu, 07 March 2013 01:52
Oracle 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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
vino06cse57 wrote on Thu, 07 March 2013 12:56
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 /


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 Go to previous messageGo to next message
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 #579074 is a reply to message #579071] Thu, 07 March 2013 07:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
... And FORMAT your post: 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.

Regards
Michel
Re: Utl_package [message #579075 is a reply to message #579073] Thu, 07 March 2013 07:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
And we still have proof about your directory and file.
Maybe your file is just empty, who knows?

Regards
Michel
Re: Utl_package [message #579076 is a reply to message #579073] Thu, 07 March 2013 07:25 Go to previous messageGo to next message
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.
Re: Utl_package [message #579144 is a reply to message #579009] Fri, 08 March 2013 03:24 Go to previous messageGo to next message
vino06cse57
Messages: 131
Registered: July 2011
Location: chennai
Senior Member
Hi All

It Worked..

Thanks
Re: Utl_package [message #579153 is a reply to message #579144] Fri, 08 March 2013 03:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
What was the problem?

Regards
Michel
Re: Utl_package [message #579170 is a reply to message #579153] Fri, 08 March 2013 05:08 Go to previous message
vino06cse57
Messages: 131
Registered: July 2011
Location: chennai
Senior Member
The input data has some delimiter problem
Previous Topic: Select INTO
Next Topic: Inserting a number of records loaded into the log table
Goto Forum:
  


Current Time: Tue Aug 26 19:21:58 CDT 2025