奇怪的 SQL 行为,为什么这个查询没有返回任何内容?
假设有一个名为“myTable”的表,包含三列:
{**ID**(PK, int, not null),
**X**(PK, int, not null),
**Name**(nvarchar(256), not null)}.
令 {4, 1, аккаунт} 为表中的一条记录。
select * from myTable as t
where t.ID=4
AND t.X = 1
AND ( t.Name = N'аккаунт' )
select * from myTable as t
where t.ID=4
AND t.X = 1
AND ( t.Name LIKE N'%аккаунт%' )
第一个查询返回记录,但是第二个查询没有返回记录? 为什么?
遇到此问题的系统:
*Windows XP - Professional - 版本 2002 - SP3
服务器排序规则: Latin1_General_CI_AS
版本: 9.00.3073.00
级别: SP2
版本: 开发人员版本
服务器排序规则: SQL_Latin1_General_CP1_CI_AS
版本: 9.00.3054.00
级别: SP2
版本:企业版
结果:
SELECT SERVERPROPERTY('SQLCharSetName')
iso_1
Using OSQL.exe
0x30043A043A04300443043D04420400000000000000000000000000000000
0x3F3F3F3F3F3F3F0000000000000000000000000000000000000000000000
0x253F3F3F3F3F3F3F25000000000000000000000000000000000000000000
SELECT CAST(name AS BINARY),
CAST(N'аккаунт' AS BINARY),
CAST(N'%аккаунт%' AS BINARY)
FROM myTable t
WHERE t.ID = 4
AND t.X = 1
CAST(name AS BINARY)
0x30043A043A04300443043D04420400000000000000000000000000000000
CAST(N'аккаунт' AS BINARY)
0x3F3F3F3F3F3F3F0000000000000000000000000000000000000000000000
CAST(N'%аккаунт%' AS BINARY)
0x253F3F3F3F3F3F3F25000000000000000000000000000000000000000000
Assume there is a table named "myTable" with three columns:
{**ID**(PK, int, not null),
**X**(PK, int, not null),
**Name**(nvarchar(256), not null)}.
Let {4, 1, аккаунт} be a record on the table.
select * from myTable as t
where t.ID=4
AND t.X = 1
AND ( t.Name = N'аккаунт' )
select * from myTable as t
where t.ID=4
AND t.X = 1
AND ( t.Name LIKE N'%аккаунт%' )
The first query return the record, however, the second does not? Why?
Systems where this issues are experienced:
*Windows XP - Professional - Version 2002 - SP3
Server Collation: Latin1_General_CI_AS
Version: 9.00.3073.00
Level: SP2
Edition: Developer Edition
Sever Collation: SQL_Latin1_General_CP1_CI_AS
Version: 9.00.3054.00
Level: SP2
Edition: Enterprise Edition
Results:
SELECT SERVERPROPERTY('SQLCharSetName')
iso_1
Using OSQL.exe
0x30043A043A04300443043D04420400000000000000000000000000000000
0x3F3F3F3F3F3F3F0000000000000000000000000000000000000000000000
0x253F3F3F3F3F3F3F25000000000000000000000000000000000000000000
SELECT CAST(name AS BINARY),
CAST(N'аккаунт' AS BINARY),
CAST(N'%аккаунт%' AS BINARY)
FROM myTable t
WHERE t.ID = 4
AND t.X = 1
CAST(name AS BINARY)
0x30043A043A04300443043D04420400000000000000000000000000000000
CAST(N'аккаунт' AS BINARY)
0x3F3F3F3F3F3F3F0000000000000000000000000000000000000000000000
CAST(N'%аккаунт%' AS BINARY)
0x253F3F3F3F3F3F3F25000000000000000000000000000000000000000000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您能否发布以下查询的结果:
这将有助于缩小问题范围。
更新:
从您的查询结果中可以看出,您的编码存在问题。
字符串常量中的西里尔文字将转换为问号 (
0x3F
)。不幸的是,我无法在测试服务器上使用
Management Studio
重现此行为。我认为
OS
设置存在一些问题,因为西里尔字符很可能甚至无法到达SQL Server
。您能否再回答三个问题:
您使用的
操作系统
是什么(版本、语言、MUI
(如果有))此查询返回什么:
选择 SERVERPROPERTY('SQLCharSetName')
使用
osql.exe
连接到您的服务器并发出此查询:选择转换(名称为二进制),
CAST(N'аккаунт' AS BINARY),
CAST(N'%аккаунт%' AS BINARY)
来自 myTable t
其中 t.ID = 4
且 tX = 1
去
在
osql.exe
中运行它会返回什么?Could you please post the result of the following query:
This will help to narrow the problem down.
UPDATE:
As I can see from the results of your query, you have a problem with encoding.
The Cyrillic literals from your string constants are being converted to the question marks (
0x3F
).Unfortunately, I cannot reproduce this behavior with
Management Studio
on my test server.I reckon there is some problem with
OS
settings, as Cyrillic characters most probably don't even reachSQL Server
.Could you please answer three more questions:
What
OS
are you using (version, language,MUI
if any)What does this query return:
SELECT SERVERPROPERTY('SQLCharSetName')
Connect to your server using
osql.exe
and issue this query:SELECT CAST(name AS BINARY),
CAST(N'аккаунт' AS BINARY),
CAST(N'%аккаунт%' AS BINARY)
FROM myTable t
WHERE t.ID = 4
AND t.X = 1
GO
What does it return being run in
osql.exe
?两个查询都为我返回相同的结果。
返回:
并
返回:(
受影响的 1 行)
我的 SQL Server 版本是:
我的排序规则设置为:
SQL_Latin1_General_CP1_CI_AS
我的 Quassnoi 结果:
0x30043A043A04300443043D04420400000000000000000000000000000000
0x30043A043A04300443043D04420400000000000000000000000000000000
0x250030043A043A04300443043D0442042500000000000000000000000000
(1 行受影响)
Both queries return the same result for me.
Returns:
And
Returns:
(1 row(s) affected)
My version of SQL Server is:
My collation is set to:
SQL_Latin1_General_CP1_CI_AS
My results for Quassnoi:
0x30043A043A04300443043D04420400000000000000000000000000000000
0x30043A043A04300443043D04420400000000000000000000000000000000
0x250030043A043A04300443043D0442042500000000000000000000000000
(1 row(s) affected)
好吧,经过大量研究,我发现这确实是在以下版本的 SQL Server 2005 上发现的问题:
Windows XP - Professional - Version 2002 - SP3
版本:9.00.3073.00
级别:SP2
版本:开发者版
版本:9.00.3054.00
级别:SP2
版本:企业版
..也可能是其他版本。
修复:升级到 SP3。
Alright, after a great deal of research, I found it is indeed a problem found on the following versions of SQL Server 2005:
Windows XP - Professional - Version 2002 - SP3
Version: 9.00.3073.00
Level: SP2
Edition: Developer Edition
Version: 9.00.3054.00
Level: SP2
Edition: Enterprise Edition
..may be other versions as well.
FIX: Upgrade to SP3.