Welcome

首页 / 数据库 / SQLServer / SQL Server的Collate语句需注意

SQL Server的Collate语句需注意2008-03-14汗,今天被Sql Server的Collate子句大玩了一把,看在线帮助不仔细!让自己绕了一个大圈,以后看MS帮助可要仔细了,事情是这样的:

下午,老大给我们发来一段SQL Script,要我们测试,看有没有错误,如有,请提出!整个Script全部在这里!

我当时就将这段脚本拉进了查询分析器,一执行,呵呵,根本没错啊!那老大为什么要发这样的邮件出来呢?于是我又切换了几个database,也没有什么问题,正当我准备测试完这一个database就放弃测试退出的时候,问题来了。错误消息如下:

Server: Msg 446, Level 16, State 9, Line 61

Cannot resolve collation conflict for equal to operation.

呵呵,有困难,找警察,咱有难,就找online啦。按下F1,键入collation,最后定位至See also中的Collate,查到帮助文件如下(不好意思,我只是将sql server2000 的在线帮助源封不动的复制了一下,当然在我当时没有看仔细的那一句我变换了颜色,各位朋友也请不要犯同样的错误为好。呵呵):

COLLATE

A clause that can be applied to a database definition or a column definition to define the collation, or to a character string expression to apply a collation cast.

Syntax

COLLATE < collation_name >

< collation_name > :: =

{ Windows_collation_name } | { SQL_collation_name }

Arguments

collation_name

Is the name of the collation to be applied to the expression, column definition, or database definition. collation_name can be only a specified Windows_collation_name or a SQL_collation_name.

Windows_collation_name

Is the collation name for Windows collation. See Windows Collation Names.

SQL_collation_name

Is the collation name for a SQL collation. See SQL Collation Names.

Remarks

The COLLATE clause can be specified at several levels, including the following:

Creating or altering a database.

You can use the COLLATE clause of the CREATE DATABASE or ALTER DATABASE statement to specify the default collation of the database. You can also specify a collation when you create a database using SQL Server Enterprise Manager. If you do not specify a collation, the database is assigned the default collation of the SQL Server instance.

Creating or altering a table column.

You can specify collations for each character string column using the COLLATE clause of the CREATE TABLE or ALTER TABLE statement. You can also specify a collation when you create a table using SQL Server Enterprise Manager. If you do not specify a collation, the column is assigned the default collation of the database.

You can also use the database_default option in the COLLATE clause to specify that a column in a temporary table use the collation default of the current user database for the connection instead of tempdb.

Casting the collation of an expression.

You can use the COLLATE clause to cast a character expression to a certain collation. Character literals and variables are assigned the default collation of the current database. Column references are assigned the definition collation of the column. For the collation of an expression, see Collation Precedence.

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. For example, two tables with names differing only in case may be created in a database with case-sensitive collation, but may not be created in a database with case-insensitive collation.

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. The identifiers for variables, GOTO labels, temporary stored procedures, and temporary tables are in the default collation of the instance.