TABLE OF CONTENTS
SQL Server – Fix @@SERVERNAME Value
Our software systems are funny and sometimes give us strange values. We don’t really dare to mess with the system level properties on our servers, but this query came from a customer we were working on a SQL Server instance for, and the result was stranger than we thought.
When working on the server and an error occurs, we usually ask for @@Version. This is standard practice for me because we don’t have to ask for the installed version, which service pack, if the server has had a CU update, etc. This output will give me all this information in black and white.
SQL SERVER – Correcting @@ServerName Property Value When Wrong Error Since I am very used to this kind of output, our client told us that the value returned by @@ServerName was NULL. This surprised us a lot and we wanted to see it in real life. This got us thinking and we needed to fix this trivial issue. Strange but I’m sure many of you have encountered this somewhere and looked for a solution.
@@ServerName refers to the local server that SQL Server is running on. This property is set during installation, but there are cases where @@ServerName can be NULL or not true.
Running the following will show the current TSQL value for this property:
SELECT @@SERVERNAME;
GO
To Fix the Problem
If the @@ServerName property is NULL, running the following will fix the problem – but only after restarting the SQL Server instance. Yes, you need an interrupt to fix this!!!
EXEC sp_addserver '<LocalServerName>', local;
GO
If the @@@ServerName property is incorrect, run the following to correct the problem:
EXEC sp_dropserver 'Old_Name';
GO
EXEC sp_addserver 'New_Name', 'local';
GO
Although the solution to this is very simple, we found this behavior strange. If you have encountered this error on your server, could you please let us know how you got here?