@@SERVERNAME, @@SERVICENAME & @@REMSERVER in SQL Server

@@SERVERNAME

This variable returns a nvarchar value that represents the name of the local server.

Select @@SERVERNAME

If the current instance is a default instance, it will return “servername”. If the instance is a named instance, then it will return “servername\instancename”.

I would prefer SERVERPROPERTY(‘servername’) over this global variable. The SERVERNAME property automatically reports changes in the network name of the computer. But, @@SERVERNAME does not report such changes automatically unless the changes are made to the local server name using the sp_addserver or sp_dropserver stored procedures.

Select  @@SERVername,SERVERPROPERTY('servername')

@@SERVICENAME

This variable returns a nvarchar value that represents the service name of SQL Server. If the current instance is a named instance, it will return the instance name else if its a default, then it reutrns “MSSQLSERVER”

Select  @@SERVICENAME

@@REMSERVER

This variable returns the name of the remote SQL Server database server as it appears in the login record.

Since am currently using SQL Server 2016, this feature returns NULL value, however , in earlier version it was returning the server name from where the procedure is being called. There is an example of how it was working in MS official documentation: https://docs.microsoft.com/en-us/sql/t-sql/functions/remserver-transact-sql?view=sql-server-ver15

This feature has been deprecated, hence its not really advisable to use in future development and also recommend to find a better way to replace in existing codes.

Honestly, I was not really able to find a good alternative for this feature and also asked this question in MS Q&A looking for an answer. Unfortunately, there are not much help on this.

If you enjoyed this blog post, feel free to share it with your friends!

One thought on “@@SERVERNAME, @@SERVICENAME & @@REMSERVER in SQL Server”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s