DB2 Import - 如何合并多个CSV文件

时间:2017-10-18 18:06:18

标签: import db2

我有几个具有相同列的CSV文件,但是列的顺序不同。

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

请帮忙解决这个导入声明?如何使此import语句与列顺序匹配?

1 个答案:

答案 0 :(得分: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.
相关问题