SQL Server 检查大小写敏感吗?

发布于 2024-08-04 22:49:24 字数 293 浏览 7 评论 0原文

如何检查 SQL Server 中的数据库是否区分大小写?我之前一直在运行查询:

SELECT CASE WHEN 'A' = 'a' THEN 'NOT CASE SENSITIVE' ELSE 'CASE SENSITIVE' END

但我正在寻找其他方法,因为这实际上在过去给我带来了问题。

编辑-更多信息: 现有产品有许多预先编写的存储过程。在存储过程中@test != @TEST取决于服务器本身的敏感度。所以我正在寻找的是检查服务器敏感性的最佳方法。

How can I check to see if a database in SQL Server is case-sensitive? I have previously been running the query:

SELECT CASE WHEN 'A' = 'a' THEN 'NOT CASE SENSITIVE' ELSE 'CASE SENSITIVE' END

But I am looking for other ways as this has actually given me issues in the past.

Edit - A little more info:
An existing product has many pre-written stored procedures. In a stored procedure @test != @TEST depending on the sensitivity of the server itself. So what I'm looking for is the best way to check the server for its sensitivity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

掐死时间 2024-08-11 22:49:24

可以在各个级别设置排序规则:

  1. 服务器
  2. 数据库

因此,您可以在不区分大小写的数据库中拥有区分大小写的列。我还没有遇到过可以为单列数据区分大小写的业务案例的情况,但我想可能会有。

检查服务器排序规则

SELECT SERVERPROPERTY('COLLATION')

检查数据库排序规则

SELECT DATABASEPROPERTYEX('AdventureWorks', 'Collation') SQLCollation;

检查列排序规则

select table_name, column_name, collation_name
from INFORMATION_SCHEMA.COLUMNS
where table_name = @table_name

Collation can be set at various levels:

  1. Server
  2. Database
  3. Column

So you could have a Case Sensitive Column in a Case Insensitive database. I have not yet come across a situation where a business case could be made for case sensitivity of a single column of data, but I suppose there could be.

Check Server Collation

SELECT SERVERPROPERTY('COLLATION')

Check Database Collation

SELECT DATABASEPROPERTYEX('AdventureWorks', 'Collation') SQLCollation;

Check Column Collation

select table_name, column_name, collation_name
from INFORMATION_SCHEMA.COLUMNS
where table_name = @table_name
小红帽 2024-08-11 22:49:24

如果您使用默认排序规则选项安装 SQL Server,您可能会发现以下查询返回相同的结果:

CREATE TABLE mytable 
( 
    mycolumn VARCHAR(10) 
) 
GO 

SET NOCOUNT ON 

INSERT mytable VALUES('Case') 
GO 

SELECT mycolumn FROM mytable WHERE mycolumn='Case' 
SELECT mycolumn FROM mytable WHERE mycolumn='caSE' 
SELECT mycolumn FROM mytable WHERE mycolumn='case' 

您可以通过在列级别强制排序规则来更改查询:

SELECT myColumn FROM myTable  
    WHERE myColumn COLLATE Latin1_General_CS_AS = 'caSE' 

SELECT myColumn FROM myTable  
    WHERE myColumn COLLATE Latin1_General_CS_AS = 'case' 

SELECT myColumn FROM myTable  
    WHERE myColumn COLLATE Latin1_General_CS_AS = 'Case' 

-- if myColumn has an index, you will likely benefit by adding 
-- AND myColumn = 'case' 

SELECT DATABASEPROPERTYEX('<database name>', 'Collation') 

由于更改此设置可能会影响应用程序和 SQL 查询,因此我将隔离先这个测试。从 SQL Server 2000 开始,您可以轻松地运行 ALTER TABLE 语句来更改特定列的排序顺序,强制其区分大小写。首先,执行以下查询以确定需要将其更改回的内容:

EXEC sp_help 'mytable' 

在默认情况下,第二个记录集应包含以下信息:

Column_Name Collat​​ion


mycolumn SQL_Latin1_General_CP1_CI_AS

无论“Collat​​ion”列返回什么,您现在都知道需要什么在进行以下更改后将其更改回,这将强制区分大小写:

ALTER TABLE mytable 
    ALTER COLUMN mycolumn VARCHAR(10) 
    COLLATE Latin1_General_CS_AS 
GO 



SELECT mycolumn FROM mytable WHERE mycolumn='Case' 
SELECT mycolumn FROM mytable WHERE mycolumn='caSE' 
SELECT mycolumn FROM mytable WHERE mycolumn='case' 

如果这把事情搞砸了,您可以将其更改回来,只需发出一个新的 ALTER TABLE 语句(确保将我的 COLLATE 标识符替换为您找到的标识符)以前):

ALTER TABLE mytable 
    ALTER COLUMN mycolumn VARCHAR(10) 
    COLLATE SQL_Latin1_General_CP1_CI_AS 

如果您无法使用 SQL Server 7.0,则可以尝试此解决方法,这可能会对性能造成更多影响(您应该只获得第一个匹配的结果):

SELECT mycolumn FROM mytable WHERE 
    mycolumn = 'case' AND 
    CAST(mycolumn AS VARBINARY(10)) = CAST('Case' AS VARBINARY(10)) 

SELECT mycolumn FROM mytable WHERE 
    mycolumn = 'case' AND 
    CAST(mycolumn AS VARBINARY(10)) = CAST('caSE' AS VARBINARY(10)) 

SELECT mycolumn FROM mytable WHERE 
    mycolumn = 'case' AND 
    CAST(mycolumn AS VARBINARY(10)) = CAST('case' AS VARBINARY(10)) 

-- if myColumn has an index, you will likely benefit by adding 
-- AND myColumn = 'case' 

If you installed SQL Server with the default collation options, you might find that the following queries return the same results:

CREATE TABLE mytable 
( 
    mycolumn VARCHAR(10) 
) 
GO 

SET NOCOUNT ON 

INSERT mytable VALUES('Case') 
GO 

SELECT mycolumn FROM mytable WHERE mycolumn='Case' 
SELECT mycolumn FROM mytable WHERE mycolumn='caSE' 
SELECT mycolumn FROM mytable WHERE mycolumn='case' 

You can alter your query by forcing collation at the column level:

SELECT myColumn FROM myTable  
    WHERE myColumn COLLATE Latin1_General_CS_AS = 'caSE' 

SELECT myColumn FROM myTable  
    WHERE myColumn COLLATE Latin1_General_CS_AS = 'case' 

SELECT myColumn FROM myTable  
    WHERE myColumn COLLATE Latin1_General_CS_AS = 'Case' 

-- if myColumn has an index, you will likely benefit by adding 
-- AND myColumn = 'case' 

SELECT DATABASEPROPERTYEX('<database name>', 'Collation') 

As changing this setting can impact applications and SQL queries, I would isolate this test first. From SQL Server 2000, you can easily run an ALTER TABLE statement to change the sort order of a specific column, forcing it to be case sensitive. First, execute the following query to determine what you need to change it back to:

EXEC sp_help 'mytable' 

The second recordset should contain the following information, in a default scenario:

Column_Name Collation


mycolumn SQL_Latin1_General_CP1_CI_AS

Whatever the 'Collation' column returns, you now know what you need to change it back to after you make the following change, which will force case sensitivity:

ALTER TABLE mytable 
    ALTER COLUMN mycolumn VARCHAR(10) 
    COLLATE Latin1_General_CS_AS 
GO 



SELECT mycolumn FROM mytable WHERE mycolumn='Case' 
SELECT mycolumn FROM mytable WHERE mycolumn='caSE' 
SELECT mycolumn FROM mytable WHERE mycolumn='case' 

If this screws things up, you can change it back, simply by issuing a new ALTER TABLE statement (be sure to replace my COLLATE identifier with the one you found previously):

ALTER TABLE mytable 
    ALTER COLUMN mycolumn VARCHAR(10) 
    COLLATE SQL_Latin1_General_CP1_CI_AS 

If you are stuck with SQL Server 7.0, you can try this workaround, which might be a little more of a performance hit (you should only get a result for the FIRST match):

SELECT mycolumn FROM mytable WHERE 
    mycolumn = 'case' AND 
    CAST(mycolumn AS VARBINARY(10)) = CAST('Case' AS VARBINARY(10)) 

SELECT mycolumn FROM mytable WHERE 
    mycolumn = 'case' AND 
    CAST(mycolumn AS VARBINARY(10)) = CAST('caSE' AS VARBINARY(10)) 

SELECT mycolumn FROM mytable WHERE 
    mycolumn = 'case' AND 
    CAST(mycolumn AS VARBINARY(10)) = CAST('case' AS VARBINARY(10)) 

-- if myColumn has an index, you will likely benefit by adding 
-- AND myColumn = 'case' 
旧梦荧光笔 2024-08-11 22:49:24

SQL Server 通过COLLATION 确定区分大小写。

COLLATION 可以在不同级别进行设置。

  1. 服务器级
  2. 数据库级
  3. 列级
  4. 表达式级

这里是 MSDN 参考。< /a>

人们可以按照 Raj More 的回答中所述检查每一级别的COLLATION

检查服务器排序规则

SELECT SERVERPROPERTY('COLLATION')

检查数据库排序规则

SELECT DATABASEPROPERTYEX('AdventureWorks', 'Collation') SQLCollation;

检查列排序规则

select table_name, column_name, collation_name
from INFORMATION_SCHEMA.COLUMNS
where table_name = @table_name

检查表达式排序规则

对于表达式级别 COLLATION 你需要看一下表达方式。 :)

它通常位于表达式的末尾,如下例所示。

SELECT name FROM customer ORDER BY name COLLATE Latin1_General_CS_AI;

排序规则描述

要获取每个COLLATION 值的描述,请尝试此操作。

SELECT * FROM fn_helpcollations()

你应该看到这样的东西。

在此处输入图像描述

您始终可以放置 WHERE 子句来过滤并仅查看您的描述整理

您可以在此处找到排序规则列表。

SQL server determines case sensitivity by COLLATION.

COLLATION can be set at various levels.

  1. Server-level
  2. Database-level
  3. Column-level
  4. Expression-level

Here is the MSDN reference.

One can check the COLLATION at each level as mentioned in Raj More's answer.

Check Server Collation

SELECT SERVERPROPERTY('COLLATION')

Check Database Collation

SELECT DATABASEPROPERTYEX('AdventureWorks', 'Collation') SQLCollation;

Check Column Collation

select table_name, column_name, collation_name
from INFORMATION_SCHEMA.COLUMNS
where table_name = @table_name

Check Expression Collation

For expression level COLLATION you need to look at the expression. :)

It would be generally at the end of the expression as in below example.

SELECT name FROM customer ORDER BY name COLLATE Latin1_General_CS_AI;

Collation Description

For getting description of each COLLATION value try this.

SELECT * FROM fn_helpcollations()

And you should see something like this.

enter image description here

You can always put a WHERE clause to filter and see description only for your COLLATION.

You can find a list of collations here.

明明#如月 2024-08-11 22:49:24

您对整理感兴趣。您可以基于此代码片段构建一些内容:

SELECT DATABASEPROPERTYEX('master', 'Collation');

更新
根据您的编辑 - 如果 @test@TEST 可以引用两个不同的变量,则它不是 SQL Server。如果您发现 same 变量不等于其自身的问题,请检查该变量是否为 NULL,因为 NULL = NULL 返回 `false。

You're interested in the collation. You could build something based on this snippet:

SELECT DATABASEPROPERTYEX('master', 'Collation');

Update
Based on your edit — If @test and @TEST can ever refer to two different variables, it's not SQL Server. If you see problems where the same variable is not equal to itself, check if that variable is NULL, because NULL = NULL returns `false.

爺獨霸怡葒院 2024-08-11 22:49:24

使用已创建的表的最佳方法是,
转到 Sql Server 查询编辑器,

输入:sp_help

这将显示表的结构,查看 COLLATE 列下所需字段的详细信息。

然后输入如下查询:

SELECT myColumn FROM myTable  
WHERE myColumn COLLATE SQL_Latin1_General_CP1_CI_AS = 'Case'

它可能是不同的字符架构 <SQL_Latin1_General_CP1_CI_AS>,因此最好找出针对该列使用的确切架构。

The best way to work with already created tables is that,
Go to Sql Server Query Editor

Type: sp_help <tablename>

This will show table's structure , see the details for the desired field under COLLATE column.

then type in the query like :

SELECT myColumn FROM myTable  
WHERE myColumn COLLATE SQL_Latin1_General_CP1_CI_AS = 'Case'

It could be different character schema <SQL_Latin1_General_CP1_CI_AS>, so better to find out the exact schema that has been used against that column.

别挽留 2024-08-11 22:49:24

如何检查 SQL Server 中的数据库是否区分大小写?

您可以使用以下查询返回您的通知数据库是否区分大小写或采用二进制排序(结果为空):

;WITH collations AS (
    SELECT 
        name,
        CASE 
            WHEN description like '%case-insensitive%' THEN 0 
            WHEN description like '%case-sensitive%' THEN 1 
        END isCaseSensitive
    FROM 
        sys.fn_helpcollations()
)
SELECT *
FROM collations
WHERE name = CONVERT(varchar, DATABASEPROPERTYEX('yourDatabaseName','collation'));

有关更多信息,请阅读 此 MSDN 信息 ;)。

How can I check to see if a database in SQL Server is case-sensitive?

You can use below query that returns your informed database is case sensitive or not or is in binary sort(with null result):

;WITH collations AS (
    SELECT 
        name,
        CASE 
            WHEN description like '%case-insensitive%' THEN 0 
            WHEN description like '%case-sensitive%' THEN 1 
        END isCaseSensitive
    FROM 
        sys.fn_helpcollations()
)
SELECT *
FROM collations
WHERE name = CONVERT(varchar, DATABASEPROPERTYEX('yourDatabaseName','collation'));

For more read this MSDN information ;).

維他命╮ 2024-08-11 22:49:24

SQL Server 不区分大小写。 SELECT * FROM SomeTableSeLeCT * frOM soMetaBLe 相同。

SQL Server is not case sensitive. SELECT * FROM SomeTable is the same as SeLeCT * frOM soMetaBLe.

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