SQL BCP导入 - 变化的列长度合并数据

时间:2016-01-08 23:04:16

标签: sql-server csv bcp

使用BCP将CSV文件上传到SQL Server我遇到了前4行包装成2行的问题,其余数据上传没有问题。第一行的最后一列与第二行的第一个条目以及一个输入(在SQL中看起来像一个空格,在记事本中显示为输入)合并。

我相信这是因为源文件(csv)省略了逗号。我不想先在Excel中加载文件。

e.g。输入

apple,orange,square,triangle [CRLF]
apple,orange,square,triangle,circle [CRLF]
apple,orange,square [CRLF]
apple,orange,square,triangle,circle,1,2,3,4,5,6,7  [CRLF]
apple,orange,square,triangle,circle,1,2,3,4,5,6,7  [CRLF]
apple,orange,square,triangle,circle,1,2,3,4,5,6,7  [CRLF]
apple,orange,square,triangle,circle,1,2,3,4,5,6,7  [CRLF] etc

e.g。输出

apple,orange,square,triangle apple,orange,square,triangle,circle,apple,orange,square,triangle,circle
apple,orange,square apple,orange,square,triangle,circle,1,2,34567
apple,orange,square,triangle,circle,1,2,3,4,5,6,7
apple,orange,square,triangle,circle,1,2,3,4,5,6,7 etc

我的BCP代码如下:

SET @command = 'bcp click_energy_reporting.dbo.tmp in \\Server\FileLocation\Combine.csv  -f \\Server\FileLocation\format_SPA1.txt -T'

我尝试合并行终止符,但我继续得到相同的结果。 -r \r

我的格式文件使用逗号分隔行。

1 个答案:

答案 0 :(得分:0)

我试图重现你的问题(SQL Server 2008 R2)。但是,对于每个值没有列的行,导入失败。所以我为每一行填充了足够的标签

测试表:

CREATE TABLE [dbo].[test_table](
    [f1] [varchar](50) NULL,
    [f2] [varchar](50) NULL,
    [f3] [varchar](50) NULL,
    [f4] [varchar](50) NULL,
    [f5] [varchar](50) NULL,
    [n1] [int] NULL,
    [n2] [int] NULL,
    [n3] [int] NULL,
    [n4] [int] NULL,
    [n5] [int] NULL,
    [n6] [int] NULL,
    [n7] [int] NULL
) ON [PRIMARY]

我放入文件C:\testfile.txt的测试数据(图片突出显示标签& CRLF):

The data file highlighting tabs & CRLF

原始数据:

apple   orange  square  triangle                                
apple   orange  square  triangle    circle                          
apple   orange  square                                  
apple   orange  square  triangle    circle  1   2   3   4   5   6   7
apple   orange  square  triangle    circle  1   2   3   4   5   6   7
apple   orange  square  triangle    circle  1   2   3   4   5   6   7
apple   orange  square  triangle    circle  1   2   3   4   5   6   7

我用于将数据导入测试表的命令:

DECLARE @cmd VARCHAR(8000);
SET @cmd='BCP TEST_TT.dbo.test_table in C:\testfile.txt -c -T';
EXEC master.sys.xp_cmdshell @cmd;

SELECT*FROM test_table的结果:

+-------+--------+--------+----------+--------+------+------+------+------+------+------+------+
|  f1   |   f2   |   f3   |    f4    |   f5   |  n1  |  n2  |  n3  |  n4  |  n5  |  n6  |  n7  |
+-------+--------+--------+----------+--------+------+------+------+------+------+------+------+
| apple | orange | square | triangle | NULL   | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| apple | orange | square | triangle | circle | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| apple | orange | square | NULL     | NULL   | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| apple | orange | square | triangle | circle | 1    | 2    | 3    | 4    | 5    | 6    | 7    |
| apple | orange | square | triangle | circle | 1    | 2    | 3    | 4    | 5    | 6    | 7    |
| apple | orange | square | triangle | circle | 1    | 2    | 3    | 4    | 5    | 6    | 7    |
| apple | orange | square | triangle | circle | 1    | 2    | 3    | 4    | 5    | 6    | 7    |
+-------+--------+--------+----------+--------+------+------+------+------+------+------+------+

成功!