Question

I am Currently downloading/updating
about 4,000 csv files that contain Stock Data(Open/High/Low/Close/Volume)
to a single Directory "Folder"

the csv files are updated daily,
new data is added every morning,
and sometimes existing data is rectified. (I am using a CsiData's Unfair Advantage, which corrects former incorrect data.)

I would like to have the "csv files" in the "Folder"
to be imported automatically into my "Oracle Database",
at a designated time.

Was it helpful?

Solution

Assuming the folder is accessible via oracle, and you have the correct permissions to see the folder Here's a rough guide to the code needed (not tested) see Ask Tom

CREATE TABLE files_to_process (file_name VARCHAR2(255));

CREATE OR REPLACE
AND COMPILE JAVA SOURCE NAMED "DirectoryList"
AS
import java.io.*;
import java.sql.*;

public class DirectoryList
{
public static void ListAllFiles(String directory)
                     throws SQLException
    {
        File path = new File( directory );
        String[] list = path.list();
        String element;

        for(int i = 0; i < list.length; i++)
        {
            element = list[i];
            sql 
            { 
                INSERT INTO Files_to_process (FILENAME) VALUES (:element) 
            };
        }
    }

}
/


CREATE OR REPLACE  PROCEDURE get_files_to_process( p_directory in varchar2 )
  AS LANGUAGE JAVA
  NAME 'DirectoryList.ListAllFiles( java.lang.String )';
/

BEGIN
    delete Files_to_process;

    get_files_to_process( '\mnt\your_folder' );

    for rec in (select file_name from Files_to_process)
    LOOP
        process_file(file_name);

    END LOOP;
END;

--so the procedure below can open the files......
CREATE OR REPLACE DIRECTORY in_files as '\mnt\your_folder';

create table stock_file_to_process as
(
--your csv structure here
--STOCKID number, 
--Open number,
--High number,
--Low number,
--Close number,
--Volume number
)
  ORGANIZATION EXTERNAL
  ( type oracle_loader
    default directory in_files
    access parameters
    ( fields terminated by ',' )
    location ('emp.dat')
  )
  /


CREATE PROCEDURE process_file(p_filename varchar2(255)) as
BEGIN
    execute immediate 'ALTER TABLE file_to_process LOCATION( p_filename )';

    for rec in (select Stockid,Open,High,Low,Close,Volume from stock_file_to_process)
    LOOP
        --your processing code here

    END LOOP;

END;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top