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
Special characters displaying incorrectly after BULK INSERT
提问by MrMatt
I'm using BULK INSERT
to 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 INSERT
works 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 INSERT
command is:
我有兴趣了解为什么会发生这种情况以及有关如何解决该问题的任何想法。该BULK INSERT
命令是:
BULK INSERT dbo.temp FROM 'C:\Temp\file.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' );
回答by Aaron Bertrand
You need to BULK INSERT
using 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'
.