2017-10-18 58 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.