Categories
Programming

Beware of @@SERVERNAME in Microsoft SQL Server

The special variable @@SERVERNAME crops up quite often on forums as the answer to the question “How do I get the name of the machine I am running on”. This is problematic, because this variable actually return the name of the SQL Server instance, which is not necessarily the same thing.

For example, if you are running on a named instance, you’ll get the following result…

SELECT @@SERVERNAME -- "YOUR-SERVER\INSTANCE"

So when you want the machine name, ask for it specifically and don’t rely on your SQL Server and machine having the same name:

SELECT SERVERPROPERTY('MACHINENAME') -- "YOUR-SERVER"