简体   繁体   English

SQL Server 2005在不区分大小写的情况下执行区分大小写的排序

[英]Sql server 2005 acting case sensitive inspite of case insensitive collation

I am having following issue.Even after case insensitive collation. 我遇到以下问题。即使不区分大小写的排序规则也是如此。 SQL server is treating @Result and @result differently. SQL Server对@Result@result处理不同。 Am i missing something.Please help. 我错过了什么吗?请帮忙。

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

SQL_Latin1_General_CP1_CI_AS

DECLARE @Result varchar(2000)    

 SELECT TOP 1 @result = addr.address_id 
   FROM dbo.address addr    
   JOIN dbo.company_address compadd ON addr.address_id = compadd.address_id    
ORDER BY addr.address_id desc  

...throws this error: ...引发此错误:

Msg 137, Level 15, State 1, Line 2
Must declare the scalar variable "@result".

Edit:- 编辑:-

This same query works in my local machine.I tried it and got no error. 同样的查询在我的本地机器上也有效,我尝试了一下,没有错误。

From MSDN: 从MSDN:

Identifier Collation 标识符归类

The collation of an identifier depends on the level at which it is defined. 标识符的排序规则取决于其定义的级别。 Identifiers of instance-level objects, such as logins and database names, are assigned the default collation of the instance. 实例级对象的标识符(例如登录名和数据库名称)被分配了实例的默认排序规则。 Identifiers of objects within a database, such as tables, views, and column names, are assigned the default collation of the database. 数据库中对象的标识符(例如表,视图和列名)被分配数据库的默认排序规则。 Variables, GOTO labels, temporary stored procedures, and temporary tables can be created when the connection context is associated with one database and then referenced when the context has been switched to another database. 当连接上下文与一个数据库关联时,可以创建变量,GOTO标签,临时存储过程和临时表,然后在上下文切换到另一数据库时引用它们。 Therefore, the identifiers for variables, GOTO labels, and temporary tables are in the default collation of the instance . 因此,变量的标识符,GOTO标签和临时表位于实例的默认排序规则中

So even though you're attempting to declare the collation of the database, variables are always going to use the default collation of your SQL Server instance. 因此,即使您尝试声明数据库的排序规则,变量始终将使用SQL Server实例的默认排序规则。

If you've just reinstalled your database into a new instance, you should consider either upgrading your code to comply with the new collation (probably what I would do), or else follow this document on how to change the instance collation . 如果您刚刚将数据库重新安装到一个新实例中,则应考虑升级代码以符合新排序规则(可能是我会做的事情),或者按照本文档的有关如何更改实例排序规则的说明进行操作

Really though, it seems a bit sloppy to have randomly cased variable references ;) 确实,随机大小写变量引用似乎有点草率;)

Collation deals with data (values); 整理处理数据(值); not identifier names. 不是标识符名称。

There is no reason for your sample to fail unless you're running the statements as separate batches as your declarations only have scope local to the batch. 除非您将语句作为单独的批处理运行,否则您的示例没有理由失败,因为声明仅具有该批处理局部的作用域。

  • If you're running the statements one-at-a-time .... there's your problem! 如果您一次运行这些语句.....那是您的问题!
  • Otherwise check what you have configured as a batch separator; 否则,请检查您配置为批量分隔符的内容; the default is GO 默认为GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM