我有以下CSV格式的数据文件 - https://www.dropbox.com/s/23dtr31pje15baz/yellow-dentist-chicago%2C%20il.csv?dl=0
一些行的数据用引号括起来表示数据中的逗号是数据的一部分而不是分隔符(参见" Bruce C. Gronner,DDS"在第一个例子的第5行)。因此,据我所知,我需要使用此post中讨论的格式文件。
首先,我想出了这个
11.0
13
1 SQLCHAR 0 1000 "\"," 1 Name SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 1000 "\"," 2 Address SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 1000 "\"," 3 State SQL_Latin1_General_CP1_CI_AS
4 SQLCHAR 0 1000 "\"," 4 Phone SQL_Latin1_General_CP1_CI_AS
5 SQLCHAR 0 1000 "\"," 5 Rating SQL_Latin1_General_CP1_CI_AS
6 SQLCHAR 0 1000 "\"," 6 Reviews SQL_Latin1_General_CP1_CI_AS
7 SQLCHAR 0 1000 "\"," 7 Website SQL_Latin1_General_CP1_CI_AS
8 SQLCHAR 0 1000 "\"," 8 Email1 SQL_Latin1_General_CP1_CI_AS
9 SQLCHAR 0 1000 "\"," 9 MerchantVerified SQL_Latin1_General_CP1_CI_AS
10 SQLCHAR 0 1000 "\"," 10 Lat SQL_Latin1_General_CP1_CI_AS
12 SQLCHAR 0 1000 "\"," 11 Long SQL_Latin1_General_CP1_CI_AS
12 SQLCHAR 0 1000 "\"," 12 ListingURL SQL_Latin1_General_CP1_CI_AS
13 SQLCHAR 0 1000 "\r\n" 13 Email2 SQL_Latin1_General_CP1_CI_AS
然后我尝试使用以下SQL代码导入
BULK INSERT RawData
FROM 'C:\Users\William\Dropbox\yellow-dentist-chicago, il'
WITH
(
FIRSTROW = 2,
FORMATFILE='C:\Users\William\Dropbox\formatfile.fmt'
);
我遇到的主要问题是只有一小部分数据用引号括起来(仅当数据中有逗号时),所以我不知道如何正确设置分隔符模式列的格式在格式文件中。任何帮助将不胜感激。
答案 0 :(得分:2)
您需要对格式文件进行细微更改。
为限制数据量,以下是我具有以下格式的数据:
Name, Address, Email
Somename,"Address one",email@email.ccc
Somename2,"Address2 Two",email@email.ccc
正如您所看到的,“地址”列使用的是双引号,“名称”和“电子邮件”不是。
使用的格式为:
10.0
3
1 SQLCHAR 0 50 ",\"" 1 Name SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 50 "\"," 2 Address SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 50 "\r\n" 3 Email2 SQL_Latin1_General_CP1_CI_AS
名称字符分隔符为,"
,格式文件为",\""
地址字符分隔符为",
,格式文件为“\”,“
答案 1 :(得分:0)
我能想到的最快捷的方式(我还有其他方法)你可以在excel中格式化csv并添加一个唯一的符号作为字段终止符(在这种情况下使用竖线条)到开头和每个单元格的结尾请参见此链接,了解如何执行此操作:
http://www.lenashore.com/2012/04/how-to-add-quotes-to-your-cells-in-excel-automatically/
您需要添加| @ |作为单元格的自定义格式而不是引号。
现在您应该可以像这样运行BULK Insert来导入数据:
BULK INSERT RequiredDB.DBO.RequiredTable
FROM '%Drive%\%Folder%\yellow-dentist-chicago-il.csv'
WITH
(
FIELDTERMINATOR = '|',
ROWTERMINATOR = '\r\n'
);
此外,我推测您在csv中发布的虚拟数据或公共信息,而不是机密或私人数据。