SQL Server Wrong Server Name


In your SQL Server code are you using @@SERVERNAME? This can return the wrong information. For me it was not returning the correct computer name, which is part of what @@SERVERNAME returns. I am not certain, but perhaps I installed SQL Server Express and then changed my Windows computer name.

What is your computer name? In Windows you can find out this information: Start, Settings, System and About. At the top od the window it will display your PC Name. At the command line you can type hostame to get your computer name.

The solution is to run 2 lines of code and then restart the SQL Server service. First, run the following code in SSMS in a new query window.

SELECT ServerProperty('machinename') as [machinename]
    ,ServerProperty('ServerName') as [ServerName]
    ,@@ServerName as [@@ServerName];

Below is the code you run in SQL Server to correct the problem. You will need to change your computer name and the SQL Server instance name from what is shown below.

EXEC sp_dropserver 'MYOLDPCNAME\SQLEXPRESS';
GO
EXEC sp_addserver 'MYNEWPCNAME\SQLEXPRESS', 'local';
GO

To restart the SQL Server service in Windows press Start, type service, click services, in the Services windows that appears, scroll down to find SQL Server (Instance Name), where Instance Name is the name of the instance of your SQL Server. Right click that and choose Restart from the pop-up. Re-run the code below in a query window in SQL Server.

SELECT ServerProperty('machinename') as [machinename]
    ,ServerProperty('ServerName') as [ServerName]
    ,@@ServerName as [@@ServerName];

The problem should be fixed.

Here is a post on Stack Overflow. Here is the Microsoft documentation.