3

我正在读这篇文章。它说 :

该字符以值 %uff1c 表示。如果将此值传递给 SQL 数据库中的 varchar 字段,它将被转换为真正的 < 字符。我还没有在 nvarchar 字段上看到这项工作

好吧,我认为这是一个 SQL Server 错误,而不是 ValidateRequest hack!如果我写%uff1c,SQL Server 必须将它保存为%uff1c. 或者,至少,它应该%uff1c由管理员选择的字符集“再次”编码。

我错了吗?

4

2 回答 2

2

It's not a bug, but a feature (but you cannot use it).

The point of the article is: if you want to post a string containing a '<', convert each < to a < (Unicode codepoint FF1C) to avoid a validation error.

SQL Server receives a Unicode character string containing < and tries to process it. If the data type of a table column or procedure parameter was Unicode-enabled (NCHAR, NVARCHAR, 2 bytes per character), no conversion would take place, and SQL Server stores the original value.

If the value is passed to a VARCHAR (1 byte per character) column or variable, however, the character < (2-byte codepoint) is transformed to a <. Extending AakashM's code to illustrate:

DECLARE @nv nvarchar, @v varchar
SET @nv = N'<'
SET @v = '<'
SELECT @nv, @v, CONVERT(varchar, @nv)

<   <   <

But, since this is the 21st century, and databases should be able to support every standardized language and symbol and character, there are very few scenarios left that justify using VARCHAR data.

于 2012-04-04T15:51:45.910 回答
1

SQL Server 不能在varchar字段中存储<。在这种情况下你希望发生什么:

DECLARE @v varchar(1)

SET @v = '<'

SELECT @v

(请注意,< 不是通常的左尖括号字符,而是带有 codepoint 的 Unicode 字符FF1C)。

上面的结果将是<(通常的左尖括号字符)。如果你想说这是一个错误,你应该说你的预期结果是什么。

于 2012-04-04T15:22:34.363 回答