FILE_IDEX (Transact-SQL)FILE_IDEX (Transact-SQL)

适用对象:是SQL Server 否Azure SQL 数据库 否Azure Synapse Analytics (SQL DW) 否并行数据仓库 APPLIES TO: yesSQL Server noAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

此函数返回当前数据库的数据、日志或全文文件的指定逻辑名称的文件标识 (ID) 号。This function returns the file identification (ID) number for the specified logical name of a data, log, or full-text file of the current database.

主题链接图标 TRANSACT-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

FILE_IDEX ( file_name )  

参数Arguments

file_name file_name
类型为 sysname 的表达式,它返回文件名称的文件 ID 值“FILE_IDEX”。An expression of type sysname that returns the file ID value 'FILE_IDEX' for the name of the file.

返回类型Return Types

intint

出现错误时,返回 NULL NULL on error

RemarksRemarks

file_name 对应于 sys.master_filessys.database_files 目录视图中的 name 列中所显示的逻辑文件名。file_name corresponds to the logical file name displayed in the name column from the sys.master_files or sys.database_files catalog views.

在 SELECT 列表、WHERE 子句或支持使用表达式的任何位置使用 FILE_IDEXUse FILE_IDEX in a SELECT list, a WHERE clause, or anywhere that supports use of an expression. 有关详细信息,请参阅表达式 (Transact-SQL)For more information, see Expressions (Transact-SQL).

示例Examples

A.A. 检索指定文件的文件 IDRetrieving the file id of a specified file

此示例返回 AdventureWorks_Data 文件的文件 ID。This example returns the file ID for the AdventureWorks_Data file.

USE AdventureWorks2012;  
GO  
SELECT FILE_IDEX('AdventureWorks2012_Data') AS 'File ID';  
GO  

下面是结果集:Here is the result set.

File ID   
-------   
1  
(1 row(s) affected)  

B.B. 在文件名未知时检索文件 IDRetrieving the file id when the file name is not known

此示例返回 AdventureWorks 日志文件的文件 ID。This example returns the file ID of the AdventureWorks log file. Transact-SQL (T-SQL) 代码段从 sys.database_files 目录视图中选择逻辑文件名称,其中文件类型等于 1(日志)。The Transact-SQL (T-SQL) code snippet selects the logical file name from the sys.database_files catalog view, where the file type equals 1 (log).

USE AdventureWorks2012;  
GO  
SELECT FILE_IDEX((SELECT TOP (1) name FROM sys.database_files WHERE type = 1)) AS 'File ID';  
GO  

下面是结果集:Here is the result set.

File ID   
-------   
2  

C.C. 检索全文目录文件的文件 IDRetrieving the file id of a full-text catalog file

此示例返回全文文件的文件 ID。This example returns the file ID of a full-text file. T-SQL 代码段从 sys.database_files 目录视图中选择逻辑文件名称,其中文件类型等于 4(全文)。The T-SQL code snippet selects the logical file name from the sys.database_files catalog view, where the file type equals 4 (full-text). 如果全文目录不存在,则此代码将返回“NULL”。This code returns 'NULL' if a full-text catalog does not exist.

SELECT FILE_IDEX((SELECT name FROM sys.master_files WHERE type = 4))  
AS 'File_ID';  

另请参阅See Also

元数据函数 (Transact-SQL) Metadata Functions (Transact-SQL)
sys.database_files (Transact-SQL) sys.database_files (Transact-SQL)
sys.master_files (Transact-SQL)sys.master_files (Transact-SQL)