How to fix @@SERVERNAME in SQL Server 2005
It seems to happen from time to time: your network engineers decide on a new network topology or naming scheme and they want to rename one or more of your SQL Server machines or worse your desktop machine!
Renaming a SQL Server instance is not as simple as renaming the computer in My Computer Properties. After you’ve restarted windows, you will find that while the instance is now available on the network with the new machine name, internally @@SERVERNAME will probably still be using the old name. This will upset a number of features within SQL Server and Management Studio and some 3rd party tools.
Using the following sql, you can see if @@SERVERNAME has the incorrect value:
1 2 3 |
SELECT @@SERVERNAME As [@@SERVERNAME], CAST(SERVERPROPERTY('MACHINENAME') AS VARCHAR(128)) + COALESCE('' + CAST(SERVERPROPERTY('INSTANCENAME') AS VARCHAR(128)), '') As RealInstanceName |
Both @@SERVERNAME and RealInstanceName should be identical. After a recent name change my results looked like this:
@@SERVERNAME RealInstanceName ——————————— ——————————— IAUTO-124F92 JKUITERS-DEV
To correct @@SERVERNAME for a default instance use the following commands:
exec sp_dropserver old_name
1 2 3 |
GO exec sp_addserver new_name, 'local' GO |
To correct @@SERVERNAME for a named instance:
1 2 3 4 |
exec sp_dropserver old_nameinstancename GO exec sp_addserver new_nameinstancename, 'local' GO |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
-- Check that the internal @@SERVERNAME name machines the machine + Instance name SELECT @@SERVERNAME As [@@SERVERNAME], CAST(SERVERPROPERTY('MACHINENAME') AS VARCHAR(128)) + COALESCE('\' + CAST(SERVERPROPERTY('INSTANCENAME') AS VARCHAR(128)), '') As RealInstanceName; -- Script to correct the @@SERVERNAME DECLARE @InternalInstanceName sysname; DECLARE @MachineInstanceName sysname; SELECT @InternalInstanceName = @@SERVERNAME, @MachineInstanceName = CAST(SERVERPROPERTY('MACHINENAME') AS VARCHAR(128)) + COALESCE('\' + CAST(SERVERPROPERTY('INSTANCENAME') AS VARCHAR(128)), ''); IF @InternalInstanceName <> @MachineInstanceName BEGIN -- Rename the instance EXEC sp_dropserver @InternalInstanceName; EXEC sp_addserver @MachineInstanceName, 'LOCAL'; END -- You now need to restart the server for the changes to take effect |