How To Create SQL Loader Control File Dynamically In Oracle

Generate SQL Loader Control File Dynamically

In my previous blog I posted to Generate SQL Plus script Dynamically and now I am providing a SQL Script to generate SQL Loader Control File dynamically from SQL script. You need to just run this script in the schema where that table exists for which you want to create the control file and after running the script it will prompt you to enter the Table name and after entering the table name it will generate the Control file for that table. You may need to edit control file after generation but a basic Control File would be created, which could be a very time saving.

Below is the SQL Plus Script from which you can generate the Control File:
[code type=”SQL”]SET ECHO OFF;
SET HEAD OFF;
SET PAGESIZE 100;
SET LINESIZE 100;
SET UNDERLINE OFF;
SET FEED OFF;
SET VER OFF;

ACCEPT tblname CHAR PROMPT ‘Enter Table Name: ‘;

SPOOL yourtable.ctl

SELECT    ‘LOAD DATA
APPEND INTO TABLE ‘
       || ‘&tblname’
       || ‘
FIELDS TERMINATED BY ”,”  OPTIONALLY ENCLOSED BY ”’
       || CHR (34)
       || ”’
TRAILING NULLCOLS (‘
  FROM DUAL
UNION ALL
SELECT tab_cols
  FROM (  SELECT column_name || ‘,’ tab_cols
            FROM user_tab_cols
           WHERE table_name = ‘&tblname’
        ORDER BY column_id)
UNION ALL
SELECT ‘)’ FROM DUAL
/
SPOOL OFF
/
[/code]
Suppose if you will generate it for Scott.Emp Table, then the output of the control file would be as following:
[code type=”SQL”]LOAD DATA                                                                                          
APPEND INTO TABLE EMP                                                                        
FIELDS TERMINATED BY ‘,’  OPTIONALLY ENCLOSED BY ‘”‘                                            
TRAILING NULLCOLS (
EMPNO,                                                                                            
ENAME,                                                                                            
JOB,                                                                                              
MGR,                                                                                              
HIREDATE,                                                                                          
SAL,                                                                                              
COMM,                                                                                              
DEPTNO                                                                                            
)                                                                                                  
[/code]

2 Comments on "How To Create SQL Loader Control File Dynamically In Oracle"

  1. How to remove the last comma from the column select list

  2. Use the below query:

    SELECT 'LOAD DATA
    APPEND INTO TABLE '
    || '&tblname'
    || '
    FIELDS TERMINATED BY '','' OPTIONALLY ENCLOSED BY '''
    || CHR (34)
    || '''
    TRAILING NULLCOLS ('
    FROM DUAL
    UNION ALL
    SELECT CASE WHEN rn = c THEN RTRIM (tab_cols, ',') ELSE tab_cols END tab_cols
    FROM ( SELECT column_name || ',' tab_cols,
    ROWNUM rn,
    (SELECT MAX (column_id)
    FROM user_tab_cols
    WHERE table_name = '&tblname')
    c
    FROM user_tab_cols
    WHERE table_name = '&tblname'
    ORDER BY column_id)
    UNION ALL
    SELECT ')' FROM DUAL

Comments are closed.