简体   繁体   English

SQL Server @@ SERVERNAME返回旧计算机名称?

[英]SQL Server @@SERVERNAME returning old machine name?

I just stumbled across an issue in my SQL Server 2008 R2 - When I call @@SERVERNAME , it's returning my OLD computer's machine name, rather than the current one. 我刚在SQL Server 2008 R2中偶然发现一个问题-调用@@SERVERNAME ,它返回的是旧计算机的计算机名,而不是当前的计算机名。 Why is this? 为什么是这样? And how can I fix it? 我该如何解决? SQL Server somehow is remembering the old machine name. SQL Server以某种方式记住了旧计算机的名称。

This is well known and documented, see Rename a Computer that Hosts a Stand-Alone Instance of SQL Server : 这是众所周知的并已记录在案,请参阅重命名承载SQL Server独立实例的计算机

When you change the name of the computer that is running SQL Server, the new name is recognized during SQL Server startup. 当您更改运行SQL Server的计算机的名称时,新名称将在SQL Server启动期间被识别。 You do not have to run Setup again to reset the computer name. 您不必再次运行安装程序来重置计算机名称。 Instead, use the following steps to update system metadata that is stored in sys.servers and reported by the system function @@SERVERNAME: 而是,使用以下步骤来更新存储在sys.servers中并由系统功能@@ SERVERNAME报告的系统元数据:

sp_dropserver <old_name>;
GO
sp_addserver <new_name>, local;
GO

You can also use SERVERPROPERTY('MachineName') which is guaranteed to always return the updated name: 您还可以使用SERVERPROPERTY('MachineName')保证始终返回更新的名称:

MachineName Windows computer name on which the server instance is running. MachineName运行服务器实例的Windows计算机名称。 For a clustered instance, an instance of SQL Server running on a virtual server on Microsoft Cluster Service, it returns the name of the virtual server. 对于群集实例,即在Microsoft群集服务上的虚拟服务器上运行的SQL Server实例,它将返回虚拟服务器的名称。

SERVERPROPERTY('ComputerNamePhysicalNetBIOS') will return the current active node in a cluster, or the same value as 'MachineName' on a non-clustered instance. SERVERPROPERTY('ComputerNamePhysicalNetBIOS')将返回集群中的当前活动节点,或与非集群实例上的'MachineName'相同的值。

Edit (by a 3rd party) to add WEFX's comment in case anyone misses it: 编辑(由第三方)以添加WEFX的评论,以防有人错过:

Also, you'll need to restart your SQL services (or reboot the SQL Server) in order for SELECT @@SERVERNAME to return the accurate (new) servername 另外,您需要重新启动SQL服务(或重新启动SQL Server),以便SELECT @@SERVERNAME返回准确的(新)服务器名。

I suspect this is because the instance was a default installation and inherited the machine name at the time and has kept it. 我怀疑这是因为实例是默认安装,并且当时继承了计算机名称并保留了该名称。 Try this? 尝试这个?

SELECT SERVERPROPERTY('MachineName')

Sometimes you can get an error There are still remote logins or linked logins for the server 'yourServerName' when you run sp_dropserver 'oldServerName'; 有时您会得到一个错误There are still remote logins or linked logins for the server 'yourServerName'运行sp_dropserver 'oldServerName';There are still remote logins or linked logins for the server 'yourServerName' sp_dropserver 'oldServerName';

If you have this kind of error then try to run sp_dropserver 'oldServerName', 'droplogins'; 如果您遇到这种错误,请尝试运行sp_dropserver 'oldServerName', 'droplogins'; instead. 代替。

I have tried all the possible solutions and the approved answer didn't work for me. 我尝试了所有可能的解决方案,但批准的答案对我不起作用。 I have surfed a bit and come up with the perfect solution. 我已经浏览了一下,并提出了完美的解决方案。 I hope somebody find this helpful. 我希望有人能对此有所帮助。

1) Open registry by Window + R key. 1)通过Window + R键打开注册表。 Type regedit 输入regedit

2) Go to *HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SQL Server* You will see plenty of numbered directories ( 100,120,130 ....) 2)转到* HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Microsoft SQL Server *您将看到大量带编号的目录(100,120,130 ....)

OR 要么

you can simply type "Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\" in the address bar of Registry Editor 您只需在注册表编辑器的地址栏中键入“ Computer \\ HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Microsoft SQL Server \\”

3) go through all the numbered directories and see if you can find " Machines " directory inside 3)浏览所有编号的目录,看看是否可以在其中找到“ Machines ”目录

4) Once you find " Machines " change the OriginalMachineName key to the server name you want it to be. 4)找到“ Machines ”后,将OriginalMachineName键更改为您想要的服务器名称。 That's actually Original machine name when the windows was first installed. 这实际上是Windows首次安装时的原始计算机名称。

PS: My path was OriginalMachineName > Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\130\\Machines PS:我的路径是OriginalMachineName> 计算机\\ HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Microsoft SQL Server \\ 130 \\ Machines

sp_addserver将修复它.....

In my case I couldn't drop the old name; 就我而言,我不能放弃名字。 I could restart MSSQL or the VM all I wanted, @@SERVERNAME kept returning the old name while every other method (including SERVERPROPERTY('SERVERNAME') ) was returning the new name, which was causing all sorts of issues. 我可以重新启动MSSQL或VM, @@SERVERNAME始终返回旧名称,而其他所有方法(包括SERVERPROPERTY('SERVERNAME') )都返回新名称,这会引起各种问题。 Turns out dropping the new name and re-adding it (and restarting MSSQL of course) fixes this odd issue: 事实证明,删除名称并重新添加(当然,然后重新启动MSSQL)可以解决此奇怪的问题:

sp_dropserver <new_name>; GO
sp_addserver <new_name>, local; GO

The database names are also case sensitive in case you ran sp_dropserver and then sp_addserver like the others said and didn't get a change with SELECT @@SERVERNAME. 如果您先运行sp_dropserver ,然后再运行sp_addserver如其他人所述)并且未使用SELECT @@ SERVERNAME进行更改,则数据库名称也区分大小写。 Just Check that you spelt the database name correctly. 只要检查您拼写正确的数据库名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM