sp_addserver (Transact-SQL)sp_addserver (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

[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]定义 本地实例的名称。Defines the name of the local instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. 重命名宿主[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]计算机后,使用**sp_addserver**通知新计算机名称的实例[!INCLUDE[ssDEnoversion](../../includes/ssdenoversion-md.md)] 。When the computer hosting [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] is renamed, use **sp_addserver** to inform the instance of the [!INCLUDE[ssDEnoversion](../../includes/ssdenoversion-md.md)] of the new computer name. [!INCLUDE[ssDE](../../includes/ssde-md.md)] 必须在该计算机承载的所有实例上执行此过程。This procedure must be executed on all instances of the [!INCLUDE[ssDE](../../includes/ssde-md.md)] hosted on the computer. [!INCLUDE[ssDE](../../includes/ssde-md.md)] 无法更改的实例名称。The instance name of the [!INCLUDE[ssDE](../../includes/ssde-md.md)] cannot be changed. 若要更改命名实例的实例名称,安装具有所需名称的新实例、从旧实例中分离数据库文件、将数据库附加到新实例并删除旧实例。To change the instance name of a named instance, install a new instance with the desired name, detach the database files from old instance, attach the databases to the new instance and drop the old instance. 或者,你可以在客户端计算机上创建客户端别名名称,无需更改服务器计算机上的实例名称即可将连接重定向到其他服务器和实例名称或 **服务器:端口** 组合。Alternatively, you can create a client alias name on the client computer, redirecting the connection to different server and instance name or **server:port** combination without changing the name of the instance on the server computer.

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

语法Syntax


sp_addserver [ @server = ] 'server' ,
     [ @local = ] 'local' 
     [ , [ @duplicate_ok = ] 'duplicate_OK' ]

参数Arguments

[ @server = ] 'server'服务器的名称。[ @server = ] 'server' Is the name of the server. MicrosoftMicrosoft 服务器名称必须唯一且必须符合 Windows 计算机名称的规则,但不允许包含空格。Server names must be unique and follow the rules for MicrosoftMicrosoft Windows computer names, although spaces are not allowed. 服务器的值为sysname,无默认值。server is sysname, with no default.

[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] 如果计算机上安装了多个 实例,则实例将如同在一个独立服务器上运行。When multiple instances of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] are installed on a computer, an instance operates as if it is on a separate server. 通过将*服务器*作为*servername\instancename*引用来指定命名实例。Specify a named instance by referring to *server* as *servername\instancename*.

[ @local = ] 'LOCAL'指定要添加为本地服务器的服务器。[ @local = ] 'LOCAL' Specifies that the server that is being added as a local server. local 的值为varchar (10),默认值为 NULL。 ** @**@local is varchar(10), with a default of NULL. 将** @local指定为 local 会 @server*定义为本地服务器的名称,并使@SERVERNAME @ 函数返回server*的值。Specifying @local as LOCAL defines @server as the name of the local server and causes the @@SERVERNAME function to return the value of server.

SQL ServerSQL Server安装程序会在安装过程中将此变量设置为计算机名称。Setup sets this variable to the computer name during installation. SQL ServerSQL Server 默认情况下,用户可通过计算机名连接到 的实例而无需额外的配置。By default, the computer name is the way users connect to an instance of SQL ServerSQL Server without requiring additional configuration.

[!INCLUDE[ssDE](../../includes/ssde-md.md)] 只有重新启动后,本地的定义才会生效。The local definition takes effect only after the [!INCLUDE[ssDE](../../includes/ssde-md.md)] is restarted. [!INCLUDE[ssDE](../../includes/ssde-md.md)]每个实例中只能定义一个本地服务器。Only one local server can be defined in each instance of the [!INCLUDE[ssDE](../../includes/ssde-md.md)].

[ @duplicate_ok = ] 'duplicate_OK'指定是否允许重复的服务器名称。[ @duplicate_ok = ] 'duplicate_OK' Specifies whether a duplicate server name is allowed. duplicate_OK 的值为varchar (13),默认值为 NULL。 ** @**@duplicate_OK is varchar(13), with a default of NULL. duplicate_OK 的值只能duplicate_OK或 NULL。 ** @**@duplicate_OK can only have the value duplicate_OK or NULL. 如果指定duplicate_OK并且要添加的服务器名称已存在,则不会引发错误。If duplicate_OK is specified and the server name that is being added already exists, no error is raised. 如果未使用命名参数, ** @** 则必须指定 local。If named parameters are not used, @local must be specified.

返回代码值Return Code Values

0(成功)或 1(失败)0 (success) or 1 (failure)

备注Remarks

若要设置或清除服务器选项,请使用sp_serveroptionTo set or clear server options, use sp_serveroption.

不能在用户定义的事务内使用sp_addserversp_addserver cannot be used inside a user-defined transaction.

使用sp_addserver添加远程服务器已停止使用。Using sp_addserver to add a remote server is discontinued. 改用sp_addlinkedserverUse sp_addlinkedserver instead.

权限Permissions

\*\* \*\* 要求具有 setupadmin 固定服务器角色的成员身份。Requires membership in the **setupadmin** fixed server role.

示例Examples

[!INCLUDE[ssDE](../../includes/ssde-md.md)] 下面的示例将承载 [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] 的计算机名称的 `ACCOUNTS\`条目更改为 。The following example changes the [!INCLUDE[ssDE](../../includes/ssde-md.md)] entry for the name of the computer hosting [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] to `ACCOUNTS`.
sp_addserver 'ACCOUNTS', 'local';

另请参阅See Also

将承载 SQL Server sp_addlinkedserver 的独立实例的计算机重命名 (transact-sql) sp_dropserver (Transact-sql) sp_helpserver transact-sql (系统存储过程)transact-sql (安全存储过程)transact-sql (Rename a Computer that Hosts a Stand-Alone Instance of SQL Server sp_addlinkedserver (Transact-SQL) sp_dropserver (Transact-SQL) sp_helpserver (Transact-SQL) System Stored Procedures (Transact-SQL) Security Stored Procedures (Transact-SQL)