2017-10-18 30 views
1

我有几个具有相同列的CSV文件,但列的顺序不同。DB2导入 - 如何合并几个CSV文件

我想通过“导入”合并所有这些CSV文件。

请你能帮助这个进口声明?我如何使这个导入语句与列顺序匹配?

+0

是Unix/Linux上运行的Windows的DB2服务器? – mao

回答

0

使用Unix/Windows上的Db2,可以使用IMPORT命令或LOAD命令。另外INGEST命令还可以使用其他方法。

使用IMPORT或LOAD,有两种方法可以使用“METHOD P”或在INSERT子句中指定目标列的顺序。下面有两个示例。

第一个例子使用“方法P”为导入:

有三个CSV文件,它们的三列是按照不同的顺序,并用三列的目标表(A,B,C):

create table mytab(a integer not null, b integer not null, c integer not null) 
DB20000I The SQL command completed successfully. 

!cat 1a.csv 
1,2,3 

!cat 1b.csv 
99,98,97 

!cat 1c.csv 
55,51,59 

import from 1a.csv of del method p(1,2,3) insert into mytab 
SQL3109N The utility is beginning to load data from file "1a.csv". 

SQL3110N The utility has completed processing. "1" rows were read from the 
input file. 

SQL3221W ...Begin COMMIT WORK. Input Record Count = "1". 

SQL3222W ...COMMIT of any database changes was successful. 

SQL3149N "1" rows were processed from the input file. "1" rows were 
successfully inserted into the table. "0" rows were rejected. 


Number of rows read   = 1 
Number of rows skipped  = 0 
Number of rows inserted  = 1 
Number of rows updated  = 0 
Number of rows rejected  = 0 
Number of rows committed = 1 


import from 1b.csv of del method p(3,2,1) insert into mytab 
SQL3109N The utility is beginning to load data from file "1b.csv". 

SQL3110N The utility has completed processing. "1" rows were read from the 
input file. 

SQL3221W ...Begin COMMIT WORK. Input Record Count = "1". 

SQL3222W ...COMMIT of any database changes was successful. 

SQL3149N "1" rows were processed from the input file. "1" rows were 
successfully inserted into the table. "0" rows were rejected. 


Number of rows read   = 1 
Number of rows skipped  = 0 
Number of rows inserted  = 1 
Number of rows updated  = 0 
Number of rows rejected  = 0 
Number of rows committed = 1 


import from 1c.csv of del method p(2,1,3) insert into mytab 
SQL3109N The utility is beginning to load data from file "1c.csv". 

SQL3110N The utility has completed processing. "1" rows were read from the 
input file. 

SQL3221W ...Begin COMMIT WORK. Input Record Count = "1". 

SQL3222W ...COMMIT of any database changes was successful. 

SQL3149N "1" rows were processed from the input file. "1" rows were 
successfully inserted into the table. "0" rows were rejected. 


Number of rows read   = 1 
Number of rows skipped  = 0 
Number of rows inserted  = 1 
Number of rows updated  = 0 
Number of rows rejected  = 0 
Number of rows committed = 1 


select * from mytab 

A   B   C   
----------- ----------- ----------- 
      1   2   3 
     97   98   99 
     51   55   59 

    3 record(s) selected. 

第二个示例使用插入的有序列目标来匹配CSV文件中的列目标顺序。

create table mynewtab(a integer not null, b integer not null, c integer not null) 
DB20000I The SQL command completed successfully. 

!cat 1a.csv 
1,2,3 

!cat 1b.csv 
99,98,97 

!cat 1c.csv 
55,51,59 

import from 1a.csv of del insert into mynewtab(a,b,c) 
SQL3109N The utility is beginning to load data from file "1a.csv". 

SQL3110N The utility has completed processing. "1" rows were read from the 
input file. 

SQL3221W ...Begin COMMIT WORK. Input Record Count = "1". 

SQL3222W ...COMMIT of any database changes was successful. 

SQL3149N "1" rows were processed from the input file. "1" rows were 
successfully inserted into the table. "0" rows were rejected. 


Number of rows read   = 1 
Number of rows skipped  = 0 
Number of rows inserted  = 1 
Number of rows updated  = 0 
Number of rows rejected  = 0 
Number of rows committed = 1 


import from 1b.csv of del insert into mynewtab(c,b,a) 
SQL3109N The utility is beginning to load data from file "1b.csv". 

SQL3110N The utility has completed processing. "1" rows were read from the 
input file. 

SQL3221W ...Begin COMMIT WORK. Input Record Count = "1". 

SQL3222W ...COMMIT of any database changes was successful. 

SQL3149N "1" rows were processed from the input file. "1" rows were 
successfully inserted into the table. "0" rows were rejected. 


Number of rows read   = 1 
Number of rows skipped  = 0 
Number of rows inserted  = 1 
Number of rows updated  = 0 
Number of rows rejected  = 0 
Number of rows committed = 1 


import from 1c.csv of del insert into mynewtab(b,a,c) 
SQL3109N The utility is beginning to load data from file "1c.csv". 

SQL3110N The utility has completed processing. "1" rows were read from the 
input file. 

SQL3221W ...Begin COMMIT WORK. Input Record Count = "1". 

SQL3222W ...COMMIT of any database changes was successful. 

SQL3149N "1" rows were processed from the input file. "1" rows were 
successfully inserted into the table. "0" rows were rejected. 


Number of rows read   = 1 
Number of rows skipped  = 0 
Number of rows inserted  = 1 
Number of rows updated  = 0 
Number of rows rejected  = 0 
Number of rows committed = 1 


select * from mynewtab 

A   B   C   
----------- ----------- ----------- 
      1   2   3 
     97   98   99 
     51   55   59 

    3 record(s) selected.