奇怪的 SQL 行为,为什么这个查询没有返回任何内容?

发布于 2024-07-15 03:36:34 字数 1522 浏览 4 评论 0原文

假设有一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

层林尽染 2024-07-22 03:36:34

您能否发布以下查询的结果:

SELECT CAST(name AS BINARY),
       CAST(N'аккаунт' AS BINARY),
       CAST(N'%аккаунт%' AS BINARY)
FROM   myTable t
WHERE  t.ID = 4  
       AND t.X = 1

这将有助于缩小问题范围。

更新:

从您的查询结果中可以看出,您的编码存在问题。

字符串常量中的西里尔文字将转换为问号 (0x3F)。

不幸的是,我无法在测试服务器上使用 Management Studio 重现此行为。

我认为 OS 设置存在一些问题,因为西里尔字符很可能甚至无法到达 SQL Server

您能否再回答三个问题:

  1. 您使用的操作系统是什么(版本、语言、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:

SELECT CAST(name AS BINARY),
       CAST(N'аккаунт' AS BINARY),
       CAST(N'%аккаунт%' AS BINARY)
FROM   myTable t
WHERE  t.ID = 4  
       AND t.X = 1

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 reach SQL Server.

Could you please answer three more questions:

  1. 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?

辞慾 2024-07-22 03:36:34

两个查询都为我返回相同的结果。

select * from myTable as t
where t.ID=4
AND t.X = 1
AND (t.Name = N'аккаунт')

返回:

ID          X           Name
----------- ----------- ------------
4           1           аккаунт

select * from myTable as t
where t.ID=4
AND t.X = 1
AND (t.Name LIKE N'%аккаунт%')

返回:(

ID          X           Name
----------- ----------- ------------
4           1           аккаунт

受影响的 1 行)

我的 SQL Server 版本是:

Microsoft SQL Server 2005 - 9.00.3077.00 (Intel X86) 
    Dec 17 2008 15:19:45 
    Copyright (c) 1988-2005 Microsoft Corporation
    Express Edition on Windows NT 5.1 (Build 2600: Service Pack 3)

我的排序规则设置为:SQL_Latin1_General_CP1_CI_AS

我的 Quassnoi 结果:
0x30043A043A04300443043D04420400000000000000000000000000000000
0x30043A043A04300443043D04420400000000000000000000000000000000
0x250030043A043A04300443043D0442042500000000000000000000000000

(1 行受影响)

Both queries return the same result for me.

select * from myTable as t
where t.ID=4
AND t.X = 1
AND (t.Name = N'аккаунт')

Returns:

ID          X           Name
----------- ----------- ------------
4           1           аккаунт

And

select * from myTable as t
where t.ID=4
AND t.X = 1
AND (t.Name LIKE N'%аккаунт%')

Returns:

ID          X           Name
----------- ----------- ------------
4           1           аккаунт

(1 row(s) affected)

My version of SQL Server is:

Microsoft SQL Server 2005 - 9.00.3077.00 (Intel X86) 
    Dec 17 2008 15:19:45 
    Copyright (c) 1988-2005 Microsoft Corporation
    Express Edition on Windows NT 5.1 (Build 2600: Service Pack 3)

My collation is set to: SQL_Latin1_General_CP1_CI_AS

My results for Quassnoi:
0x30043A043A04300443043D04420400000000000000000000000000000000
0x30043A043A04300443043D04420400000000000000000000000000000000
0x250030043A043A04300443043D0442042500000000000000000000000000

(1 row(s) affected)

北方。的韩爷 2024-07-22 03:36:34

好吧,经过大量研究,我发现这确实是在以下版本的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文