COL_LENGTH (Transact-SQL)COL_LENGTH (Transact-SQL)

適用範圍:Applies to: 是SQL ServerSQL Server (所有支援的版本) yesSQL ServerSQL Server (all supported versions) 是Azure SQL DatabaseAzure SQL DatabaseYesAzure SQL DatabaseAzure SQL Database適用範圍:Applies to: 是SQL ServerSQL Server (所有支援的版本) yesSQL ServerSQL Server (all supported versions) 是Azure SQL DatabaseAzure SQL DatabaseYesAzure SQL DatabaseAzure SQL Database

此函式傳回資料行的定義長度 (以位元組為單位)。This function returns the defined length of a column, in bytes.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

COL_LENGTH ( 'table' , 'column' )   

注意

若要檢視 SQL Server 2014 與更早版本的 Transact-SQL 語法,請參閱舊版文件To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.

引數Arguments

' table '' table '
我們要判斷其資料行長度資訊的資料表名稱。The name of the table whose column length information we want to determine. table 是類型為 nvarchar 的運算式。table is an expression of type nvarchar.

' column '' column '
我們要判斷其長度的資料行名稱。The column name whose length we want to determine. column 是類型為 nvarchar 的運算式。column is an expression of type nvarchar.

傳回類型Return type

smallintsmallint

例外狀況Exceptions

發生錯誤或呼叫端沒有檢視物件的正確權限時,會傳回 NULL。Returns NULL on error, or if a caller does not have the correct permission to view the object.

SQL ServerSQL Server 中,使用者只能檢視使用者擁有或被授與某些權限之安全性實體的中繼資料。In SQL ServerSQL Server, a user can only view the metadata of securables that the user owns, or on which the user has been granted permission. 這表示,如果使用者沒有物件的正確權限,則發出中繼資料的內建函式 (例如 COL_LENGTH) 可能會傳回 NULL。This means that metadata-emitting, built-in functions such as COL_LENGTH might return NULL, if the user does not have correct permission on the object. 如需詳細資訊,請參閱中繼資料可見性設定See Metadata Visibility Configuration for more information.

備註Remarks

對於使用 max 規範 (varchar(max)) 來宣告的 varchar 資料行,COL_LENGTH 會傳回值 -1。For varchar columns declared with the max specifier (varchar(max)), COL_LENGTH returns the value -1.

範例Examples

此範例顯示 varchar(40) 類型之資料行以及 nvarchar(40) 類型之資料行的傳回值:This example shows the return values for a column of type varchar(40) and a column of type nvarchar(40):

USE AdventureWorks2012;  
GO  
CREATE TABLE t1(c1 VARCHAR(40), c2 NVARCHAR(40) );  
GO  
SELECT COL_LENGTH('t1','c1')AS 'VarChar',  
      COL_LENGTH('t1','c2')AS 'NVarChar';  
GO  
DROP TABLE t1;  

以下為結果集。Here is the result set.

VarChar     NVarChar  
40          80  

另請參閱See also

運算式 (Transact-SQL)Expressions (Transact-SQL)
中繼資料函式 (Transact-SQL)Metadata Functions (Transact-SQL)
COL_NAME (Transact-SQL)COL_NAME (Transact-SQL)
COLUMNPROPERTY (Transact-SQL)COLUMNPROPERTY (Transact-SQL)