如何在所有值周围批量插入双引号的 csv?
我正在尝试将 .csv 文件插入 SQL Server 2008 R2。
.csv 大小为 300+MB,来自 http://ipinfodb.com/ip_database.php 完整 (城市),400万条记录。
这是前 5 行,其中第一行 = 列标题:
"ip_start";"country_code";"country_name";"region_code";"region_name";"city";"zipcode";"latitude";"longitude";"metrocode"
"0";"RD";"Reserved";;;;;"0";"0";
"16777216";"AU";"Australia";;;;;"-27";"133";
"17367040";"MY";"Malaysia";;;;;"2.5";"112.5";
"17435136";"AU";"Australia";;;;;"-27";"133";
我尝试了导入和导出数据以及批量插入,但还无法正确导入它们。
我应该使用 bcp 吗?它可以处理剥离 ""
吗?如何?
非常感谢。
I am trying to insert a .csv file into SQL Server 2008 R2.
The .csv is 300+MB from http://ipinfodb.com/ip_database.php Complete
(City), 4.0M records.
Here're the top 5 lines, with 1st line = column headers:
"ip_start";"country_code";"country_name";"region_code";"region_name";"city";"zipcode";"latitude";"longitude";"metrocode"
"0";"RD";"Reserved";;;;;"0";"0";
"16777216";"AU";"Australia";;;;;"-27";"133";
"17367040";"MY";"Malaysia";;;;;"2.5";"112.5";
"17435136";"AU";"Australia";;;;;"-27";"133";
I tried Import and Export Data, and BULK INSERT, but haven't been able to import them correctly yet.
Shall I resort to use bcp? can it handle stripping the ""
? how?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
明白了,忘记将文本限定符设置为
"
:Got it, forgot to set Text Qualifier as
"
:您的数据看起来非常不一致,因为 NULL 值也不带有引号。
我相信您可以创建一个格式文件来自定义您的特定 csv 文件及其在 SQL SERVER 中的特定终止符。
在这里查看更多内容:
http://lanestechblog.blogspot.com/ 2008/08/sql-server-bulk-insert-using-format.html
Your data looks pretty inconsistent since NULL values don't also carry a quotation enclosure.
I believe you can create a format file to customize to your particular csv file and its particular terminators in SQL SERVER.
See more here:
http://lanestechblog.blogspot.com/2008/08/sql-server-bulk-insert-using-format.html
这是单次导入还是您想要安排定期导入?如果这是一项一次性任务,您应该能够使用导入和导出向导。文本限定符将是引号 ("),请务必选择第一个数据行中的列名称,并且您需要传达字段分隔符是分号 (;)。
I不确定文件的格式是否正确 - 每个数据行后面的最后一个分号可能有问题,如果您遇到任何错误,只需向文件添加一个新的列标题
即可:我刚刚做了一个快速测试,末尾的分号将被视为该行中最终值的一部分,我建议在标题(第一行)末尾添加一个 ;"tempheader" - 这将导致 SQL 将最后的分号视为分隔符,并且导入完成后,您可以删除该额外的列。
Is this a single import or are you wanting to schedule a recurring import? If this is a one-time task, you should be able to use the Import and Export Wizard. The text qualifier will be the quotation mark ("), be sure to select column names in the first data row, and you'll want to convey that the field delimiter is the semicolon (;).
I'm not certain the file is properly formatted - the last semicolon following each of the data rows might be a problem. If you hit any errors, simply add a new column header to the file.
EDIT: I just did a quick test, the semicolons at the end will be treated as part of the final value in that row. I would suggest adding a ;"tempheader" at the end of your header (first) row - that will cause SQL to treat the final semicolon as a delimiter and you can delete that extra column once the import is complete.
在 C# 中,你可以使用这段代码,为我工作
In C# you can use this code, working for me