46 个 SQL 加载程序的控制文件

发布于 2024-09-24 16:52:17 字数 141 浏览 6 评论 0原文

我必须使用 SQL Loader for Oracle 加载 46 个表的数据。所有数据文件均为 CSV。

CSV 中的列顺序与表中的列顺序匹配。

我需要为每个表创建一个控制文件。

我批量生产这些文件的最佳方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无名指的心愿 2024-10-01 16:52:17

我知道这是一个老问题,但它仍然是一个相关的问题。对于未来的搜索者,这里是我添加到实用程序包中的一个过程,用于生成表的框架控制文件。将表名称传递给它,它会输出一个默认控制文件,您可以在需要时调整该文件。您可能需要首先进行编辑以满足您的需求。

像这样调用:

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

过程:

/********************************************************************************************************
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

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
飘逸的'云 2024-10-01 16:52:17

我做过类似的事情(大约 120 个表,超过 1500 列)。我用的是excel(因为我的工作结果是一个excel文件)。
按照 Adam Musch 的建议,我从 Excel 宏中读取数据库中的表元,然后填写到表格中。
我想这对你也有用。
或者您可以使用您熟悉的语言创建一个工具来执行相同的操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文