带有可选文本限定符的批量插入 [英] Bulk Insert with optional text qualifier

查看:887
本文介绍了带有可选文本限定符的批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用批量插入将csv导入到db。
它是逗号分隔的csv文件。所有字段均不包含文本限定符。

I am importing csv to db using bulk insert. It is the comma delimited csv file. No text qualifiers for all fields.

但某些字段可能包含逗号作为数据的一部分。
用于例如ADDRESS字段值。这些值用双引号括起来。
仅当字段值中有逗号时,双引号才会出现,否则值不会用双引号括起来。
因此在某些行中,ADDRESS值用双引号括起来,但在其他行中它们不是。
是否有一种方法可以在批量插入命令中指定文本限定符?

But some fields may have comma as part of the data. for eg, ADDRESS field value. Those values are surronded with double quotes. Those double quotes appear only if the field value has comma in it otherwise values are not surronded with double quotes. So in some rows ADDRESS values are surronded with double-quotes, but in other rows they are not. Is there a way to specify the text-qualifier in the bulk insert command?

我尝试过批量插入格式文件选项。

I tried bulk insert with format file option.

BULK INSERT Test_Imported FROM 'C:\test.csv' 
WITH (FIRSTROW=0,FIELDTERMINATOR = ',',ROWTERMINATOR = '\n',FORMATFILE = 'C:\test.Fmt')

方式我可以在格式文件中提及双引号作为可选文本限定符。

but there is no way i can mention the double quotes as optional text qualifiers in the format file.

PS:
此函数实际上是较大模块的一部分,写在c#。
批量插入命令从c#调用。

PS: this function is actually a part of the bigger module, which is written in c#. bulk insert command is called from c#.

csv文件来自另一个自动化系统的电子邮件。我没有对csv文件的格式
的控制。有大约150列。
每个csv文件中平均有12000行。
忘记对数据库进行spcify。这是SQL Server 2005.

The csv file is coming by email from another automated system. i have no control over the format of the csv file.There are around 150 columns. In average 12000 rows are coming in each csv file. Forgot to spcify the DB. It is SQL server 2005.

推荐答案

不幸的是,你必须预处理文件才能保持一致。
SQL批量操作拆分字段分隔符上的字符串。

Unfortunately, you'll have to pre-process the file to make it consistent. SQL bulk operations split the string on the field delimiter.

一些选项:


  • 在c#中进行处理,将没有引号包围的逗号更改为竖线(|)

  • 在2:和非文件中分割文件。只有在相同字段包含

时,才能使用此选项。您说您无法控制格式,

You say you have no control over the format, but what you have is unusable...

这篇关于带有可选文本限定符的批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆