我需要在sql server 2014中导入一个utf-8 csv文件
因为不支持utf-8,最好的解决方案是我们utf-16,我将文件转换为utf-16 BE
在Notepad ++中,rowterminator显示为CR LF
并使用以下查询
BULK INSERT [test].[dbo].[csv_test]
FROM 'C:\inetpub\wwwroot\test\16052017.csv'
WITH
(
FIELDTERMINATOR = ','
,ROWTERMINATOR = '\n'
,BATCHSIZE = 50000
,DATAFILETYPE = 'widechar'
,CODEPAGE = '1201'
,FIRSTROW = 2
,TABLOCK
)
这只能部分起作用,因为似乎无法识别“某些”终结符而是导入51行仅导入25,而在最新字段中导入前一行。
我尝试将FieldTerminator和RowTerminator分别更改为',\ 0'和'\ n \ 0'以及\ n \ 0 \ r \ 0' 但最好的结果是简单的'\ n',但据说只有半行
我做错了什么?
好吧,抱歉它有效..问题是我添加的ID字段:它创造了所有混乱
但是因为我会使用格式文件来避免某些行..我将程序翻译如下:
CREATE PROCEDURE dbo.spImportCSV_test
(
@FilePath nvarchar(200)='C:\inetpub\wwwroot\test\16052017.csv',
@FirstRow nvarchar(5)='2',
@FMT nvarchar(20)='csv_test'
)
AS
BEGIN
Declare @Query nvarchar(max),
@Tablename nvarchar(100)='test.dbo.csv_test'
SET @Query = 'BULK INSERT '
+ @Tablename
+ ' FROM '''
+ @FilePath
+ ''' WITH ( FORMATFILE = ''C:\inetpub\wwwroot\test\fmt\'+@FMT+'.fmt'', FIRSTROW = '+@FirstRow+')';
print @Query
EXEC (@query);
END
其中csv_test.fmt是:
12.0
13
1 SQLNCHAR 0 500 "," 1 RegisteredItemN Latin1_General_100_CI_AS
2 SQLNCHAR 0 500 "," 2 CountryOfDestination Latin1_General_100_CI_AS
3 SQLNCHAR 0 500 "," 3 Customer Latin1_General_100_CI_AS
4 SQLNCHAR 0 500 "," 4 Identifier Latin1_General_100_CI_AS
5 SQLNCHAR 0 500 "," 5 Freight Latin1_General_100_CI_AS
6 SQLNCHAR 0 500 "," 6 HAWB Latin1_General_100_CI_AS
7 SQLNCHAR 0 500 "," 7 MAWB Latin1_General_100_CI_AS
8 SQLNCHAR 0 500 "," 8 Name Latin1_General_100_CI_AS
9 SQLNCHAR 0 500 "," 9 Address Latin1_General_100_CI_AS
10 SQLNCHAR 0 500 "," 10 Postcode Latin1_General_100_CI_AS
11 SQLNCHAR 0 500 "," 11 City Latin1_General_100_CI_AS
12 SQLNCHAR 0 500 "," 12 Weight Latin1_General_100_CI_AS
13 SQLNCHAR 0 500 "\n" 13 Connote Latin1_General_100_CI_AS
但是我收到了这个错误:
Bulk load data conversion error (truncation) for row 2, column 1 (RegisteredItemN).
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".