@@SERVERNAME 在多实例环境中使用 sqlpackage.exe 时

@@SERVERNAME when using sqlpackage.exe in multi instance environment

我们正在使用 sqlpackage.exe 部署数据库。在部署过程中,我们有一个创建环回链接服务器的脚本:

EXEC master.dbo.sp_addlinkedserver 
  @server = N'loopback'
 ,@srvproduct=N''
 ,@provider=N'SQLNCLI'
 ,@datasrc=@@SERVERNAME -- This is the important part!

我们面临的问题是,在部署过程中,@@SERVERNAME 评估为默认实例,而不是指定的目标服务器,这是一个命名实例。我认为问题的根源在于部署是“以”默认实例的名义发生的...?

我试图找到这个问题的解决方案,例如为 sqlpackage.exe 指定一个参数来解决这个“问题”,但到目前为止还没有成功。我们的快速解决方案是首先使用 SSMS 在目标服务器中执行特定脚本,使用正确的服务器创建环回,然后进行部署,但显然我们对更“抗性”的解决方案感兴趣。

我在 docs.microsoft.com / Questions 上得到了答案:

The only way I have found that you can create a linked server using @@SERVERNAME in the script is to delete the definition of the Linked Server as an object and then put that script in a PreDeployment script and it will pick up the ServerName with the instance and create the linked server.

解决方案已验证,在 PreDeployment 脚本中 @@SERVERNAME 评估目标服务器(命名实例),而不是默认服务器。