How to get current instance name from T-SQL

Guillermo Gutiérrez picture Guillermo Gutiérrez · Aug 6, 2013 · Viewed 212.9k times · Source

How can I get the SQL Server server and instance name of the current connection, using a T-SQL script?

Answer

Guillermo Gutiérrez picture Guillermo Gutiérrez · Aug 6, 2013

Just found the answer, in this SO question (literally, inside the question, not any answer):

SELECT @@servername

returns servername\instance as far as this is not the default instance

SELECT @@servicename

returns instance name, even if this is the default (MSSQLSERVER)