カラム順が異なるテーブルへのデータインポート

(8.0.27)

※complete-insertオプション付与によりカラム定義もinsert文に含まれる

drop table tab1;
create table tab1(col1 int,col2 int);
insert into tab1 values(1,2);
select * from tab1;

\! mysqldump --complete-insert -uroot -p  -t test tab1 > tab1.dmp
\! cat tab1.dmp

→「INSERT INTO `tab1` (`col1`, `col2`) VALUES (1,2);」

drop table tab1;
create table tab1(col2 int,col1 int);

source tab1.dmp

select * from tab1;

→ダンプ取得時にcomplete-insertオプション付与すれば、カラム順が異なっていても、データインポートは可能

(19c)

drop table tab1 purge;
create table tab1(col1 int,col2 int);
insert into tab1 values(1,2);
commit;
select * from tab1;

!expdp test/test@pdb1 directory=ORA_DIR dumpfile=a.dmp logfile=a.log tables=TAB1 reuse_dumpfiles=yes

※impdpのSQLFILEオプションはDDLのみ書き込むため、ダンプファイル内のinsert文は確認不可

drop table tab1 purge;
create table tab1(col2 int,col1 int);

!impdp test/test@pdb1 directory=ORA_DIR dumpfile=a.dmp logfile=a.log tables=TAB1 content=data_only

select * from tab1;

→デフォルト設定で、カラム順が異なっていても、データインポートは可能

(14)

drop table tab1;
create table tab1(col1 int,col2 int);
insert into tab1 values(1,2);
select * from tab1;

\! pg_dump -Fp -a -t tab1 test> tab1.dmp

\! cat tab1.dmp

→「COPY public.tab1 (col1, col2) FROM stdin;」

drop table tab1;
create table tab1(col2 int,col1 int);


\i tab1.dmp

※ダンプ読込によりsearch_pathがクリアされている

select * from public.tab1;

→デフォルト設定で、カラム順が異なっていても、データインポートは可能

(2019)
https://docs.microsoft.com/ja-jp/sql/tools/bcp-utility?view=sql-server-ver15
https://memorandom-nishi.hatenablog.jp/entry/2017/03/23/194013


drop table tab1;
create table tab1(col1 int,col2 int);
insert into tab1 values(1,2);
select * from tab1;

 

フォーマットファイルの作成
bcp test.dbo.tab1 format nul -f "C:\tab1.fmt" -T -c -t,

フォーマットファイルを使用してエクスポート
bcp test.dbo.tab1 out "C:\tab1.dmp"  -T  -t, -f "C:\tab1.fmt" -o "C:\tab1.log"

 

tab1.fmtをインポート先のテーブル定義に合わせて修正

14.0
2
1       SQLCHAR             0       12      ","      1     col1         ""
2       SQLCHAR             0       12      "\r\n"   2     col2         ""

14.0
2
1       SQLCHAR             0       12      ","      2     col2         ""
2       SQLCHAR             0       12      "\r\n"   1     col1         ""


Server column order と Server column nameを入れ換える

 

drop table tab1;
create table tab1(col2 int,col1 int);

フォーマットファイルを使用してインポート
bcp test.dbo.tab1 in "C:\tab1.dmp"  -T  -t, -f "C:\tab1.fmt" -o "C:\tab1.log"


select * from tab1;

→フォーマットファイルを使用すれば、カラム順が異なっていても、データインポートは可能