1 つのオプションは、SQLLoader を使用してファイルをテーブルにロードすることです。
3 つのテーブルを作成したとします。
CREATE TABLE tableA(
col1 int, col2 int, col3 int, col4 int
);
CREATE TABLE tableB(
col1 int, col2 varchar2(100), col3 int, col4 int, col5 int, col6 varchar2(100)
);
CREATE TABLE tableC(
col1 varchar2(100), col2 date, col3 number(10,2)
);
ファイルには常に 1 つの形式 (3 つの可能な形式のうちの 1 つ) のレコードが含まれていると想定しています。
このような場合、フォーマットごとに 3 つの異なる制御ファイルを作成できます:
format_a.ctl
load data
infile 'c:\tmp\test\file.txt'
into table tableA
fields terminated by "|"
( col1, col2, col3, col4 )
format_b.ctl
load data
infile 'c:\tmp\test\file.txt'
into table tableB
fields terminated by "|"
( col1, col2, col3, col4, col5, col6 )
format_c.ctl
infile 'c:\tmp\test\file.txt'
into table tableC
fields terminated by "|"
( col1 ,
col2 date 'yyyy-mm-dd',
col3 )
次に、ファイルの形式を検出し、適切な制御ファイルを使用してデータをアップロードする簡単なスクリプトを作成します。これは Windows 環境の例です。
@echo off
set filename=file.txt
IF NOT EXIST %filename% GOTO error
findstr /M "\|.*\|.*\|.*\|.*\|" file.txt
IF NOT ERRORLEVEL 1 GOTO formatB
findstr /M "\|.*\|.*\|" file.txt
IF NOT ERRORLEVEL 1 GOTO formatA
findstr /M "\|.*\|" file.txt
IF NOT ERRORLEVEL 1 GOTO formatC
:error
Echo Error: file %filename% doesn't exist or doesn't match any proper format
goto end
:formatA
set ctl_file=format_a
goto import
:formatB
set ctl_file=format_b
goto import
:formatc
set ctl_file=format_c
goto import
:import
echo Import using: %ctl_file%
sqlldr test/test@//192.168.2.51:1521/orcl control=%ctl_file%.ctl log=%ctl_file%.log
:end
この行で:
sqlldr test/test@//192.168.2.51:1521/orcl control=%ctl_file%.ctl log=%ctl_file%.log
test/test@ は、test
パスワードを持つデータベース ユーザーです。test