Check Server Name in SQL Server
In SQL Server, you can check the server name using the @@SERVERNAME system variable or the SYS.SERVERS system view.
Syntax:
-- Using @@SERVERNAME system variable
SELECT @@SERVERNAME AS ServerName;
-- Using SYS.SERVERS system view
SELECT name AS ServerName FROM SYS.SERVERS WHERE server_id = 0; Example 1: Check Server Name using @@SERVERNAME
Let's say we want to check the server name of the current SQL Server instance.
Query:
SELECT @@SERVERNAME AS ServerName; Result:
| ServerName |
|---|
| MY_SERVER_NAME |
The result will display the name of the current SQL Server instance.
Example 2: Check Server Name using SYS.SERVERS
Let's say we want to check the server name of the current SQL Server instance using the SYS.SERVERS system view.
Query:
SELECT name AS ServerName FROM SYS.SERVERS WHERE server_id = 0; Result:
| ServerName |
|---|
| MY_SERVER_NAME |
The result will display the name of the current SQL Server instance.
Example 3: Check Server Name with Instance Name
Let's say we want to check the server name with the instance name.
Query:
SELECT @@SERVERNAME AS ServerName, @@SERVICENAME AS InstanceName ; Result:
| ServerName | InstanceName |
|---|---|
| MY_SERVER_NAME | MSSQLSERVER |
The result will display the server name and instance name of the current SQL Server instance.
