46 Control Files for SQL Loader

2019-07-17 07:33发布

问题:

I have to load 46 tables with data using SQL Loader for Oracle. All the data files are CSV.

The column order in the CSV matches the column order in the table.

I need to create a control file for each table.

What is the best way for me to mass produce these files?

回答1:

I know this is an old question, but it is still a relevant question. For future searchers here is a procedure I added to our utility package that generates a skeleton control file for a table. Pass it the table name and it outputs a default control file that you can tweak where needed. You may need to edit to fit your needs first.

Call like this:

set serveroutput on;
exec utils.gen_ctl_file('TABLE_NAME');

The procedure:

/********************************************************************************************************
Name:       GEN_CTL_FILE

Desc:       Generates a skeleton control file from a table for loading data via SQL*Loader.

Args:       tablename_in IN VARCHAR2, delim_in VARCHAR2 DEFAULT '|'

Returns:    None.

Usage:      utils.gen_ctl_file('tablename');

Notes:      Prints a skeleton control file.

            If a template for a fixed-length data file is desired, use 'FIXED' for the delim_in string.

            Example usage:

            set serveroutput on;
            execute utils.gen_ctl_file('tablename');

************************************************************************************************************************/
PROCEDURE GEN_CTL_FILE(tablename_in IN VARCHAR2, delim_in VARCHAR2 DEFAULT '|') IS
ERRNULLTABLENAME     CONSTANT NUMBER        := -20103; -- User-defined error numbers and messages.
ERRNULLTABLENAMEMSG  CONSTANT VARCHAR2(100) := 'A table name is required.';
USAGE                CONSTANT VARCHAR2(100) := '*   USAGE: UTILS.GEN_CTL_FILE(tablename_in IN VARCHAR2, fieldsep_in VARCHAR2 DEFAULT ''|'')';
v_delim                       VARCHAR2(20)  := NVL(delim_in, '|');
err_nbr  NUMBER;
err_msg  VARCHAR2(1000);

CURSOR COL_CUR  IS
  SELECT COLUMN_NAME, 
  DECODE(COLUMN_ID, 1, ' ', ',') || RPAD(COLUMN_NAME, 32) || case upper(v_delim)
    when 'FIXED' then 'POSITION(99:99) '
    else NULL
  end|| DECODE(DATA_TYPE,
         'VARCHAR2', 'CHAR NULLIF(' || COLUMN_NAME || '=BLANKS)',
         'CHAR', 'CHAR NULLIF(' || COLUMN_NAME || '=BLANKS)',
         'FLOAT', 'DECIMAL EXTERNAL NULLIF(' || COLUMN_NAME || '=BLANKS)',
         'NUMBER', DECODE(                                                 DATA_PRECISION,
         0, 'INTEGER EXTERNAL NULLIF (' || COLUMN_NAME || '=BLANKS)',
         DECODE(DATA_SCALE, 0, 'INTEGER EXTERNAL NULLIF (' || COLUMN_NAME || '=BLANKS)', 'DECIMAL EXTERNAL NULLIF (' || COLUMN_NAME || '=BLANKS)')),
         'DATE', 'DATE "MM/DD/YYYY" NULLIF (' || COLUMN_NAME || '=BLANKS)',
         data_type)
           AS COL_DATA
  FROM  USER_TAB_COLUMNS
  WHERE TABLE_NAME = UPPER(tablename_in)
  ORDER BY COLUMN_ID;

BEGIN

IF tablename_in IS NULL THEN
  RAISE_APPLICATION_ERROR(ERRNULLTABLENAME, ERRNULLTABLENAMEMSG || CHR(10) || USAGE);
END IF;

DBMS_OUTPUT.PUT_LINE('--');
DBMS_OUTPUT.PUT_LINE('-- NOTE - When using DIRECT=TRUE to perform block inserts to a table,');
DBMS_OUTPUT.PUT_LINE('--        the table''s triggers will not be used! Plan accordingly to');
DBMS_OUTPUT.PUT_LINE('--        manually perform the trigger actions after loading, if needed.');
DBMS_OUTPUT.PUT_LINE('--');
DBMS_OUTPUT.PUT_LINE('OPTIONS (DIRECT=TRUE)');
DBMS_OUTPUT.PUT_LINE('UNRECOVERABLE');
DBMS_OUTPUT.PUT_LINE('LOAD DATA');
DBMS_OUTPUT.PUT_LINE('APPEND');
DBMS_OUTPUT.PUT_LINE('INTO TABLE ' || UPPER(tablename_in));
DBMS_OUTPUT.PUT_LINE('EVALUATE CHECK_CONSTRAINTS');
if upper(v_delim) != 'FIXED' then
  DBMS_OUTPUT.PUT_LINE('FIELDS TERMINATED BY ''' || v_delim || ''' TRAILING NULLCOLS');
end if;
DBMS_OUTPUT.PUT_LINE('(');

-- The cursor for loop construct implicitly opens and closes the cursor.
FOR COL IN COL_CUR
LOOP
  DBMS_OUTPUT.PUT_LINE(COL.COL_DATA);
END LOOP;
DBMS_OUTPUT.PUT_LINE(')' || CHR(10));

EXCEPTION
WHEN OTHERS THEN
  err_nbr  := SQLCODE;
  err_msg  := SUBSTR(SQLERRM, 1, 1000);
  -- if any error occurs, print the SQLCODE message.
  DBMS_OUTPUT.PUT_LINE('ERROR: ' || err_nbr || ' occurred: ' || err_msg);
END; -- GEN_CTL_FILE


回答2:

I have been do similar thing (about 120 tables with over 1500 columns). I used excel (because my working result is an excel file).
From Excel macro I read tables meta from DB as Adam Musch suggested, then fill into the Sheets.
I think this will work for you too.
Or you can create a tool use you familiar language to do the same.