How can I get the SQL Server server and instance name of the current connection, using a T-SQL script?
I found this:
EXECUTE xp_regread
@rootkey = 'HKEY_LOCAL_MACHINE',
@key = 'SOFTWARE\Microsoft\Microsoft SQL Server',
@value_name = 'InstalledInstances'
That will give you list of all instances installed in your server.
The
ServerName
property of theSERVERPROPERTY
function and@@SERVERNAME
return similar information. TheServerName
property provides the Windows server and instance name that together make up the unique server instance.@@SERVERNAME
provides the currently configured local server name.
And Microsoft example for current server is:
SELECT CONVERT(sysname, SERVERPROPERTY('servername'));
This scenario is useful when there are multiple instances of SQL Server installed on a Windows server, and the client must open another connection to the same instance used by the current connection.