MS SQL 插入问号而不是非 latyn 符号
MS SQL inserts question marks instead of non-latyn symbols
我正在使用 Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64)
Aug 22 2017 17:04:49
Copyright (C) 2017 Microsoft Corporation
Developer Edition (64-bit) on Windows Server 2012 R2 Standard Evaluation 6.3 <X64> (Build 9600: ) (Hypervisor)
当我这样做时:INSERT INTO MYTABLE(COLNAME) VALUES ('Қазақстан')
它会插入问号 ?????????
而不是实际的字符串。我的专栏类型是 NVARCHAR(255)
。如果我像这样在我的值之前插入 N
字符:...(N'Қазақстан')
它将被正确保留。这是插入非 ASCII 字符的唯一方法还是我应该更改其他内容?
谢谢!
你的问题的答案是肯定的,你应该使用N
来自Docs
When prefixing a string constant with the letter N, the implicit conversion will result in a UCS-2 or UTF-16 string if the constant to convert does not exceed the max length for the nvarchar string data type (4,000). Otherwise, the implicit conversion will result in a large-value nvarchar(max).
因此,如果您不使用 N
,则 SQL 服务器会将其视为 VARCHAR
(非 unicode)。
使用 N
会将字符串转换为 unicode 字符串。
我正在使用 Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64)
Aug 22 2017 17:04:49
Copyright (C) 2017 Microsoft Corporation
Developer Edition (64-bit) on Windows Server 2012 R2 Standard Evaluation 6.3 <X64> (Build 9600: ) (Hypervisor)
当我这样做时:INSERT INTO MYTABLE(COLNAME) VALUES ('Қазақстан')
它会插入问号 ?????????
而不是实际的字符串。我的专栏类型是 NVARCHAR(255)
。如果我像这样在我的值之前插入 N
字符:...(N'Қазақстан')
它将被正确保留。这是插入非 ASCII 字符的唯一方法还是我应该更改其他内容?
谢谢!
你的问题的答案是肯定的,你应该使用N
来自Docs
When prefixing a string constant with the letter N, the implicit conversion will result in a UCS-2 or UTF-16 string if the constant to convert does not exceed the max length for the nvarchar string data type (4,000). Otherwise, the implicit conversion will result in a large-value nvarchar(max).
因此,如果您不使用 N
,则 SQL 服务器会将其视为 VARCHAR
(非 unicode)。
使用 N
会将字符串转换为 unicode 字符串。