0

I am currently facing an issue(below is the error code) when trying to import data from a .csv file to OracleDB. Now, I have a stored procedure so that I could easily call it out. Now, every time I try to run or call the stored procedure it gives me an error(error code below)

So this is my stored procedure called load_csv_into_employee;

CREATE OR REPLACE PROCEDURE load_csv_into_employee AS
   v_count NUMBER;
BEGIN
   DBMS_OUTPUT.PUT_LINE('Checking if the external table already exists...');

   -- Check if the external table already exists
   SELECT COUNT(*)
   INTO v_count
   FROM user_tables
   WHERE table_name = 'CSV_TABLE';

   -- If the external table doesn't exist, create it
   IF v_count = 0 THEN
      DBMS_OUTPUT.PUT_LINE('Creating the external table...');
      EXECUTE IMMEDIATE '
         CREATE TABLE csv_table (
            EMPLOYEE_ID VARCHAR2(50),
            FIRST_NAME VARCHAR2(100),
            LAST_NAME VARCHAR2(100),
            DEPARTMENT VARCHAR2(200)
         )
         ORGANIZATION EXTERNAL (
            TYPE ORACLE_LOADER
            DEFAULT DIRECTORY csv_dir
            ACCESS PARAMETERS (
               RECORDS DELIMITED BY NEWLINE
               FIELDS TERMINATED BY '',''
               MISSING FIELD VALUES ARE NULL
            )
            LOCATION (''mssql_data.csv'')
         )';
   END IF;

   -- Insert data into EMPLOYEE
   DBMS_OUTPUT.PUT_LINE('Inserting data into EMPLOYEE...');
   EXECUTE IMMEDIATE '
      INSERT INTO EMPLOYEE
      SELECT * FROM csv_table';

   -- Drop the external table
   DBMS_OUTPUT.PUT_LINE('Dropping the external table...');
   EXECUTE IMMEDIATE 'DROP TABLE csv_table';

   DBMS_OUTPUT.PUT_LINE('Committing changes...');
   COMMIT;
EXCEPTION
   WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END load_csv_into_employee;
/

However it gives me this error every time I execute it: Error: ORA-29913: error in executing ODCIEXTTABLEOPEN callout.

I tried(Most of the answers I searched they based on permission issue):

  1. I have tried researching here and found that there might be a permission issues. So I made sure that I have created the csv_dir using the syntax CREATE DIRECTORY csv_dir AS '\\dir\path\your_file.csv'; and also made sure that I have granted it with permission. GRANT READ, WRITE ON DIRECTORY csv_dir TO MyUserName;. I used the sys as sysdba user to grant permission for this one

Note: I am currently just trying to test and learn so I had to use the SYS user.

EDIT: I have added print on every execution, so it stops on the Inserting data into EMPLOYEE and then the error shows up.

enter image description here

2
  • 1
    It’s generally a good idea to make sure statements that you want to run dynamically work correctly statically first - so create and query the external table manually and get that working before trying to do it in a procedure. Do you really need to create and drop the table at runtime? Why not just create it manually once - then you can avoid dynamic SQL. Commented May 6, 2024 at 10:39
  • 1
    Remove the exception handler and re-run the procedure. Instead of printing out only the top of the error stack, let Oracle raise and display the entire error stack. I bet there's a more useful error message underneath the generic ORA-29913. Commented May 6, 2024 at 19:46

1 Answer 1

0

The location is relative to the directory object, not the full path; so

LOCATION (''\\directory\path\your_file.csv'')

should just be

LOCATION (''your_file.csv'')

and the directory object is the path to the OS directory/folder (which has to be on the database server), so:

CREATE DIRECTORY csv_dir AS '\\dir\path';
Sign up to request clarification or add additional context in comments.

4 Comments

Hello Alex, I appreciate the help. I have updated the location path and now it is LOCATION (''mssql_data.csv''). However it still produces the same error code.
Is the directory on the database server, rather than a client machine? You showed the directory creation with the file name as part of that path, which isn't correct - but not sure if that's a mistake writing the question?
You are correct, when I created and executed the create directory, I have included the file name as well. Now, I have dropped the csv_dir directory and created a new one with a directory path only \\dir\path like what you have mentioned in your reply. However, the same error still shows. I have double checked it before replying to you. Thank you!
You need to re-grant the read/write privilege ls too. Is that directory on the database server? Does the user Oracle runs under have read/write permissions at operating system level, on the directory and file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.