SQL BULK INSERT 后特殊字符显示不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13996967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:46:13  来源:igfitidea点击:

Special characters displaying incorrectly after BULK INSERT

sqlsql-server-2012bulkinsertnvarchar

提问by MrMatt

I'm using BULK INSERTto import a CSV file. One of the columns in the CSV file contains some values that contain fractions (e.g. 1m?f).

我正在使用BULK INSERT导入 CSV 文件。CSV 文件中的一列包含一些包含分数的值(例如1m?f)。

I don't need to do any mathematical operations on the fractions, as the values will just be used for display purposes, so I have set the column as nvarchar. The BULK INSERTworks but when I view the records within SQL the fraction has been replaced with a cent symbol () so the displayed text is 1m¢f.

我不需要对分数进行任何数学运算,因为这些值仅用于显示目的,因此我将列设置为nvarchar. 该BULK INSERT作品,但是当我SQL中查看记录的部分已被替换一分钱符号(),因此显示的文本1m¢f

I'm interested to understand why this is happening and any thoughts on how to resolve the issue. The BULK INSERTcommand is:

我有兴趣了解为什么会发生这种情况以及有关如何解决该问题的任何想法。该BULK INSERT命令是:

BULK INSERT dbo.temp FROM 'C:\Temp\file.csv' 
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' );

回答by Aaron Bertrand

You need to BULK INSERTusing the CODEPAGE = 'ACP', which converts string data from Windows codepage 1252 to SQL Server codepage.

您需要BULK INSERT使用CODEPAGE = 'ACP',它将字符串数据从 Windows 代码页 1252 转换为 SQL Server 代码页。

BULK INSERT dbo.temp FROM 'C:\Temp\file.csv' 
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', CODEPAGE = 'ACP');

If you are bringing in UTF-8 data on a new enough version of SQL Server:

如果要在足够新的 SQL Server 版本上引入 UTF-8 数据:

[...] , CODEPAGE = '65001');

You may also need to specify DATAFILETYPE = 'char|native|widechar|widenative'.

您可能还需要指定DATAFILETYPE = 'char|native|widechar|widenative'.