Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read text file to insert data into Oracle SQL table

I am studying Oracle SQL developer.

What I am doing is reading text file line by line from the folder. Then Inserting data to the SQL table.

I am able to compile my PROCEDURE, however, it doesn't seem to be inserting the data to file.

Create or Replace PROCEDURE Rfile is
f UTL_FILE.FILE_TYPE;
s VARCHAR2(200); 
BEGIN
f := UTL_FILE.FOPEN('C:\Projects\','testdatabinary.txt','R');
  IF UTL_FILE.IS_OPEN(f) THEN
    LOOP
      BEGIN
        UTL_FILE.GET_LINE(f,s);
        IF s IS NULL THEN
          EXIT;
        END IF;
      INSERT INTO DATAINSERT
      (COLUMN1, COLUMN2)
      VALUES
      (s, 'testdatabinary');
      END;
  END LOOP;
  COMMIT;
  END IF;
 END;

And I have a table DATAINSERT with two varchar(200) type cols

I am not really sure the reasons that PROCEDURE is not inserting data to table

I just checked error message

Error starting at line 1 in command:
EXEC Rfile
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at "SYSTEM.RFILE", line 5
like image 538
Dc Redwing Avatar asked Dec 07 '22 09:12

Dc Redwing


1 Answers

Not sure what causing problems. For me its working fine here is my example code


--Reference Site --https://community.oracle.com/thread/3633577?start=0&tstart=0

     set serveroutput on;
     CREATE or replace DIRECTORY USER_DIR AS '/home/oracle'; 
     GRANT READ ON DIRECTORY USER_DIR TO PUBLIC;

     DECLARE 
        V1 VARCHAR2(200); --32767
        F1 UTL_FILE.FILE_TYPE; 
     BEGIN 
        F1 := UTL_FILE.FOPEN('USER_DIR','temp.txt','R'); 
        Loop
        BEGIN
    UTL_FILE.GET_LINE(F1,V1); 
    dbms_output.put_line(V1);
    EXCEPTION WHEN No_Data_Found THEN EXIT; END;
        end loop;

        IF UTL_FILE.IS_OPEN(F1) THEN
     dbms_output.put_line('File is Open');
        end if;

        UTL_FILE.FCLOSE(F1); 
     END; 
     /
    set serveroutput off;

like image 60
laks Avatar answered Dec 14 '22 22:12

laks