SQL Server 检查大小写敏感吗?
如何检查 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
可以在各个级别设置排序规则:
因此,您可以在不区分大小写的数据库中拥有区分大小写的列。我还没有遇到过可以为单列数据区分大小写的业务案例的情况,但我想可能会有。
检查服务器排序规则
检查数据库排序规则
检查列排序规则
Collation can be set at various levels:
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
Check Database Collation
Check Column Collation
如果您使用默认排序规则选项安装 SQL Server,您可能会发现以下查询返回相同的结果:
您可以通过在列级别强制排序规则来更改查询:
由于更改此设置可能会影响应用程序和 SQL 查询,因此我将隔离先这个测试。从 SQL Server 2000 开始,您可以轻松地运行 ALTER TABLE 语句来更改特定列的排序顺序,强制其区分大小写。首先,执行以下查询以确定需要将其更改回的内容:
在默认情况下,第二个记录集应包含以下信息:
Column_Name Collation
mycolumn SQL_Latin1_General_CP1_CI_AS
无论“Collation”列返回什么,您现在都知道需要什么在进行以下更改后将其更改回,这将强制区分大小写:
如果这把事情搞砸了,您可以将其更改回来,只需发出一个新的 ALTER TABLE 语句(确保将我的 COLLATE 标识符替换为您找到的标识符)以前):
如果您无法使用 SQL Server 7.0,则可以尝试此解决方法,这可能会对性能造成更多影响(您应该只获得第一个匹配的结果):
If you installed SQL Server with the default collation options, you might find that the following queries return the same results:
You can alter your query by forcing collation at the column level:
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:
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:
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):
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):
SQL Server 通过
COLLATION
确定区分大小写。COLLATION
可以在不同级别进行设置。这里是 MSDN 参考。< /a>
人们可以按照 Raj More 的回答中所述检查每一级别的
COLLATION
。检查服务器排序规则
检查数据库排序规则
检查列排序规则
检查表达式排序规则
对于表达式级别
COLLATION
你需要看一下表达方式。 :)它通常位于表达式的末尾,如下例所示。
排序规则描述
要获取每个
COLLATION
值的描述,请尝试此操作。你应该看到这样的东西。
您始终可以放置
WHERE
子句来过滤并仅查看您的描述整理
。您可以在此处找到排序规则列表。
SQL server determines case sensitivity by
COLLATION
.COLLATION
can be set at various levels.Here is the MSDN reference.
One can check the
COLLATION
at each level as mentioned in Raj More's answer.Check Server Collation
Check Database Collation
Check Column Collation
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.
Collation Description
For getting description of each
COLLATION
value try this.And you should see something like this.
You can always put a
WHERE
clause to filter and see description only for yourCOLLATION
.You can find a list of collations here.
您对整理感兴趣。您可以基于此代码片段构建一些内容:
更新
根据您的编辑 - 如果
@test
和@TEST
可以引用两个不同的变量,则它不是 SQL Server。如果您发现 same 变量不等于其自身的问题,请检查该变量是否为NULL
,因为NULL = NULL
返回 `false。You're interested in the collation. You could build something based on this snippet:
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 isNULL
, becauseNULL = NULL
returns `false.使用已创建的表的最佳方法是,
转到 Sql Server 查询编辑器,
输入:
sp_help
这将显示表的结构,查看 COLLATE 列下所需字段的详细信息。
然后输入如下查询:
它可能是不同的字符架构 <
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 :
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.您可以使用以下查询返回您的通知数据库是否区分大小写或采用二进制排序(结果为空):
有关更多信息,请阅读 此 MSDN 信息 ;)。
You can use below query that returns your informed database is case sensitive or not or is in binary sort(with null result):
For more read this MSDN information ;).
SQL Server 不区分大小写。
SELECT * FROM SomeTable
与SeLeCT * frOM soMetaBLe
相同。SQL Server is not case sensitive.
SELECT * FROM SomeTable
is the same asSeLeCT * frOM soMetaBLe
.