Thursday, July 28, 2011

SELECT @@SERVERNAME, it may return NULL / Wrong Server name

when you run SELECT @@SERVERNAME, it may return NULL / Wrong Server name.

Whenever you change the Network Name of Server Name, @@SERVERNAME does not report such changes. @@SERVERNAME reports changes made to the local server name using the sp_addserver or sp_dropserver stored procedure.

To confirm do the following:
SELECT @@SERVERNAME
select SERVERPROPERTY('MACHINENAME')
If the two above outputs shown different name means do the following:

To make SELECT @@SERVERNAME reflect the new changed Network Name perform the below task.

USE master
GO
EXEC sp_dropserver 'OLDSERVERNAME'
GO
EXEC SP_addserver 'NEWNETWORKNAME','LOCAL'
GO
Once done please restart the SQL Services and then Query SELECT @@SERVERNAME.

5 comments:

  1. The 'LOCAL' parameter fixed it for me. Thanks!

    ReplyDelete
  2. When I do the above, my select @@servername result still shows as NULL but when I add the
    EXEC SP_addserver 'NEWNETWORKNAME','LOCAL'
    GO
    I get a message that my server already exists even after a restart! Why is this? This is a cloned sql server from a server to a VM so I'm wondering if that is why.

    ReplyDelete
  3. Hi, I have a problem... This command (sp_dropserver), send the msg:

    "Msg 15190, Level 16, State 1, Procedure sp_dropserver, Line 56
    There are still remote logins or linked logins for the server 'SERVERNAME'"

    How deleting this logins?

    Thanks!

    ReplyDelete
    Replies
    1. Mine still says Null as well, I tried:
      sp_addserver 'YourServer', 'Local', 'duplicate_ok'
      Restarted all services
      And still no difference.

      Select CONVERT(char(20), SERVERPROPERTY('servername') returns the correct name though...

      Delete
    2. QUOTE
      Hi, I have a problem... This command (sp_dropserver), send the msg:

      "Msg 15190, Level 16, State 1, Procedure sp_dropserver, Line 56
      There are still remote logins or linked logins for the server 'SERVERNAME'"

      How deleting this logins?
      QUOTE


      USE master
      GO
      EXEC sp_dropserver 'OLDSERVERNAME', droplogins
      GO

      Delete