我正在尝试将以下数据库(位于txt文件中)导入到我的SQL Server数据库中:
Maxmind Free World Cities Database
我已经定义了一个合适的表,然后尝试了以下查询来导入:
BULK INSERT Cities FROM 'C:\Users\***************\worldcitiespop.txt'
WITH (FIRSTROW = 2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
但是出现了以下错误:
Msg 4866,Level 16,State 8,Line 1批量加载失败。专栏 在第1行第7列的数据文件中太长。验证是否 字段终止符和行终止符是正确指定的。消息7301, 16级,状态2,行1无法获得所需的接口 (“IID_IColumnsInfo”)来自OLE DB提供程序“BULK”,用于链接服务器 “(空)”。
似乎认为没有新的行行终止符。 如何正确导入数据库?
答案 0 :(得分:1)
首先使用此内容创建worldcitiespop.fmt
文件(文件最后需要一个空行):
10.0
7
1 SQLCHAR 0 10 "," 1 Country SQL_Latin1_General_Cp437_BIN
2 SQLCHAR 0 100 "," 2 City SQL_Latin1_General_Cp437_BIN
3 SQLCHAR 0 100 "," 3 AccentCity SQL_Latin1_General_Cp437_BIN
4 SQLCHAR 0 100 "," 4 Region SQL_Latin1_General_Cp437_BIN
5 SQLCHAR 0 10 "," 5 Population SQL_Latin1_General_Cp437_BIN
6 SQLCHAR 0 20 "," 6 Latitude SQL_Latin1_General_Cp437_BIN
7 SQLCHAR 0 20 "\n" 7 Longitude SQL_Latin1_General_Cp437_BIN
然后像这样加载:
INSERT INTO Cities
SELECT a.Country,
a.City,
a.AccentCity,
a.Region,
CAST(a.[Population] as int) as [Population],
CAST(a.Latitude as decimal(10,7)) as Latitude,
CAST(a.Longitude as decimal(10,7)) as Longitude
FROM OPENROWSET(
BULK 'D:\worldcitiespop.txt', FORMATFILE = 'D:\worldcitiespop.fmt', FIRSTROW = 2
) AS a;
在我的旧笔记本上,此插页持续约5分钟