Skip to main content

Querying Data From Flat Files in Oracle

Couple of days before i had an interesting discussion with my client related to Oracle external tables,

Client: Is there any way to read the data from flat file(s) which are stored outside the database?

Raja: Could you please explain in more detail?

Client: Sure. We have data analytics team, what they will do is, they will export the file(s) (.csv/.txt) from their system and push it to our file system (Oracle directory). 

They have a job to push their files to our file system, which will run in predefined interval. Once it's uploaded we should read the file(s) data and display it in our system.

Raja: Yes! it's very much possible, if we go with Oracle external table.

Client: What is that?

Raja: It can be used for query, join and sort operations. External tables allow oracle to query data that is stored outside the database in flat files. Once it's running we can create views against it.

Client: Oh! Sounds interesting. That's what i needed. Is there any limitations on external tables?

Raja: Yes. No DML can be performed on external tables.

Client: That's Ok. It's not needed at all, since we are not going to perform any DML operation. So you please go ahead.

-----------------------------------------------------------

Here I am giving an example to read the data that is stored outside the database in flat files.

Step 1: File(s) should be saved to the file system available to the Oracle server.
Fig 1: Sample load file data (.CSV)
Fig 2: Sample load file data (.TXT)

Step 2: Create a directory, that should pointing to the location of the file.

Step 3: Understand the load file structure, which oracle external table going to read.

Step 4: Create the external table as per the load file structure.

CREATE TABLE fxgn_emp_ext_data
  (
    empno      NUMBER,
    ename      VARCHAR2(240),
    JOB        VARCHAR2(240),
    department VARCHAR2(240),
    LOCATION   VARCHAR2(240),
    manager    VARCHAR2(240),
    hiredate   DATE,
    salary     NUMBER,
    commission NUMBER
  )
  ORGANIZATION EXTERNAL
  (
    TYPE oracle_loader 
    DEFAULT DIRECTORY APEXUAT -- put your directory name here
    ACCESS PARAMETERS (
                        fields terminated BY ',' optionally enclosed BY '"'
                        missing field VALUES are NULL 
                      ( empno,
                        ename,
                        JOB,
                        department,
                        LOCATION,
                        manager,
                        hiredate,
                        salary,
                        commission) 
                      ) 
    LOCATION ('employee_details.csv') 
-- put your file(s) name here, which should be saved in appropriate directory
  )
  REJECT LIMIT UNLIMITED;

Step 6: Query the external table the following result will be displayed.

Fig 3: Output
Note: Once external table is created, it can be queried like a regular table.

ORA-29913: If the load file have not been saved in the appropriate directory the following result will be displayed.
Fig 4: ORA-29913
That's it. This is mind blowing.

Happy APEXing!!!...

References:

Comments

  1. We just upgraded our Oracle database to 19c and discovered that external tables have a new requirement that broke our import process! There has to be at least one line-feed character at the end of the column header, or there need to be data rows. Our files are sometimes just the column headers with no rows. Unfortunately our vendor doesn't put a line-feed at the end in those cases! So we've had to rewrite the code! :-(

    ReplyDelete
  2. There are many blogs I have read. But when I read Your Blogs I have found such useful information, fresh content with such amazing editing everything is superb in your blog. Thank you so much for sharing this useful and informative information with us.

    online medicine delivery in ajmer

    ReplyDelete

Post a Comment

Popular posts from this blog

Printing Page Numbers in RTF Template [Oracle BI Publisher]

Here I am giving an example to print the page numbers dynamically in the RTF (Rich Text Format) template. Step 1:  Go to page footer and copy and paste the below script. Page |  <?fo:page-number?>  of  <?fo:page-number-citation:xdofo:lastpage-joinseq?> <fo:page-number> :   This is the object, which is used to represent the current page-number. <?fo:page-number-citation:xdofo:lastpage-joinseq?> :  This is the syntax, which is used to represent the total number of pages. Step 2:  Load the XML and preview the result. Output: That's it. References: fo:page-number Printing Page Number Code in Oracle XMLP RTF Template

Oracle Application Express Views (APEX)

Application Express Views Search SELECT * FROM apex_dictionary WHERE column_id = 0; View Comment Parent View APEX_APPLICATIONS Applications defined in the current workspace or database user. APEX_WORKSPACES APEX_APPLICATION_ALL_AUTH All authorization schemes for all components by Application APEX_APPLICATIONS

Save Selected Interactive Grid Records into a Collection - Oracle APEX

Here I am giving an example to save selected interactive grid records into a oracle apex collection. Step 1: Create a new blank page. Note: Mine was page 20. You will need to update reference to " P20 " with your page number if it's different. Step 2: Create a new interactive grid report region to the page using below query. Set Static Id "EmpDetails" to the region. SELECT  *     FROM   ( SELECT  emp . empno ,                emp . ename ,                emp . JOB ,                dept . dname department ,                dept . loc  LOCATION ,                mgr . ename  manager ,                emp . hiredate ,                 nvl ( emp . sal , 0 )  salary ,                 nvl ( emp . comm , 0 )  commission            FROM  eba_demo_chart_emp emp ,                eba_demo_chart_dept dept ,                eba_demo_chart_emp mgr           WHERE  emp . deptno = dept . deptno             AND  emp . mgr      = mgr . empno  ( + )           ORDER   BY  emp . ename