BulkInsert导入CSV文件
通过IP计算网站访问者的IP来源,常常使用http://www.maxmind.com/app/geolitecountry提供的CSV数据库
1. 建表
create table CountryIP2 ( BeginIP varchar(200), EndIP varchar(200), BeginIPNumber varchar(200), EndIPNumber varchar(200), CountryCode varchar(200), CountryName varchar(200) )
2. 利用Bulk Insert快速的插入大批量数据
bulk insert CountryIP2 from 'D:\2.csv' WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' )
但是这个过程可能会出现如下问题:
Msg 4866, Level 16, State 1, Line 1
The bulk load failed. The column is too long in the data file for row 1, column 6. Verify that the field terminator and row terminator are specified correctly.
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 1Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
用记事本打开csv文件,发现它的每一条数据并不是一行一行的显示。
解决办法:用Excel打开CSV,然后另存为,会提示一个对话框,选择是。然后再用记事本打开csv文件,发现每一条数据都已经换好行了。