You might find the question duplicate to these 2 links (Link1 and Link2) but they haven't been answered correctly. Let me brief you the question what i really want.
Looking For
I am looking for a way through which we can check if the provided SQL Instance Name is locally available or is residing on another remote computer.
My local SQL Server Instance name is Office-PC\SQLEXPRESS
Solutions i found which didn't work
1) Source : When i use the below code it says remote while the specified instance is present local.
SELECT Case when HOST_NAME()=SERVERPROPERTY('Office-PC\SQLEXPRESS') then 'local' else 'remote' end
2) I tried using the below code suggested in many comments on different websites. But it is showing incorrect output.
print HOST_NAME() //OUTPUT : Office-PC
print @@SERVERNAME //OUTPUT : HOME\SQLEXPRESS
3) Some people suggested to use the below command to get instance name which works perfectly but it doesn't tell us which of the instance is locally available.
SQLCMD -L
4) I also read somewhere (can't find the link) that we can get the Instance name from Registry itself.
That is all i could find.
If anyone has any other ways to find if the provided named SQL Instance is locally available, then please let me know.
HOST_NAME()and@@SERVERNAMEwork for me. If I check on a remote connection I get two different results; if I check on the actual server where the SQL Server instance lives I get two identical results. But this is because the SQL Server instances where I work are named after the server they run on. So this comes down to configuration?SELECT client_net_address, local_net_address FROM sys.dm_exec_connections WHERE session_id = @@SPIDshould do. Ifclient_net_addressis<local machine>orclient_net_address = local_net_address, then you are connected to a local server. Disclaimer: not thoroughly tested with all possible transports.