程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SQL Server的Collate語句需注意

SQL Server的Collate語句需注意

編輯:關於SqlServer

汗,今天被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.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved