@@SERVERNAME (Transact-SQL)@@SERVERNAME (Transact-SQL)

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

返回正在运行 SQL ServerSQL Server 的本地服务器的名称。Returns the name of the local server that is running SQL ServerSQL Server.
文章链接图标 Transact-SQL 语法约定Article link icon Transact-SQL Syntax Conventions

语法Syntax

  
@@SERVERNAME  

返回类型Return Types

nvarcharnvarchar

备注Remarks

SQL ServerSQL Server 安装程序在安装时将服务器名设置为计算机名。Setup sets the server name to the computer name during installation. 若要更改服务器的名称,请使用 sp_addserver,然后重新启动 SQL ServerSQL ServerTo change the name of the server, use sp_addserver, and then restart SQL ServerSQL Server.

安装了多个 SQL ServerSQL Server 实例时,如果本地服务器名称自安装后未发生更改,则 @@SERVERNAME 返回以下本地服务器名称信息。With multiple instances of SQL ServerSQL Server installed, @@SERVERNAME returns the following local server name information if the local server name has not been changed since set up.

实例Instance 服务器信息Server information
默认实例Default instance 'servername''servername'
命名实例Named instance 'servername\instancename''servername\instancename'
故障转移群集实例 - 默认实例failover cluster instance - default instance 'network_name_for_fci_in_wsfc''network_name_for_fci_in_wsfc'
故障转移群集实例 - 命名实例failover cluster instance - named instance 'network_name_for_fci_in_wsfc\instancename''network_name_for_fci_in_wsfc\instancename'

尽管 @@SERVERNAME 函数和 SERVERPROPERTY 函数的 SERVERNAME 属性可能返回相似格式的字符串,但信息会有所不同。Although the @@SERVERNAME function and the SERVERNAME property of SERVERPROPERTY function may return strings with similar formats, the information can be different. SERVERNAME 属性自动报告计算机网络名的更改。The SERVERNAME property automatically reports changes in the network name of the computer.

相比之下,@@SERVERNAME 不报告此更改。In contrast, @@SERVERNAME does not report such changes. @@SERVERNAME 报告使用 sp_addserversp_dropserver 存储过程对本地服务器名所做的更改。@@SERVERNAME reports changes made to the local server name using the sp_addserver or sp_dropserver stored procedure.

示例Examples

下面的示例显示了使用 @@SERVERNAMEThe following example shows using @@SERVERNAME.

SELECT @@SERVERNAME AS 'Server Name'  

下面是一个结果集示例。Here is a sample result set.

Server Name  
---------------------------------  
ACCTG  
  

另请参阅See Also

配置函数 (Transact-SQL) Configuration Functions (Transact-SQL)
SERVERPROPERTY (Transact-SQL) SERVERPROPERTY (Transact-SQL)
sp_addserver (Transact-SQL)sp_addserver (Transact-SQL)