How to get current instance name from T-SQL

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

+50
sql sql-server tsql sql-server-2008r2-express
Aug 6 '13 at 0:57
source share
5 answers

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

SELECT @@servername 

returns server_name \ instance if it is not the default instance

 SELECT @@servicename 

returns the name of the instance, even if it is the default value (MSSQLSERVER)

+104
Aug 6 '13 at 1:11
source share

How about this:

 EXECUTE xp_regread @rootkey='HKEY_LOCAL_MACHINE', @key='SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQl', @value_name='MSSQLSERVER' 

This will also get the instance name. null means the default instance:

 SELECT SERVERPROPERTY ('InstanceName') 

http://technet.microsoft.com/en-us/library/ms174396.aspx

+12
Aug 6 '13 at 1:03
source share

SELECT @@servername will provide you with data as server/instanceName

To get only instanceName , you must run select @@ServiceName .

+7
May 18 '15 at 10:49
source share

I found this:

 EXECUTE xp_regread @rootkey = 'HKEY_LOCAL_MACHINE', @key = 'SOFTWARE\Microsoft\Microsoft SQL Server', @value_name = 'InstalledInstances' 

This will give you a list of all instances installed on your server.




The ServerName property of the SERVERPROPERTY and @@SERVERNAME returns similar information. The ServerName property provides the Windows server name and instance name, which together make up a unique server instance. @@SERVERNAME contains the local server name currently configured.

And a Microsoft example for the current server:

 SELECT CONVERT(sysname, SERVERPROPERTY('servername')); 

This scenario is useful when there are multiple instances of SQL Server on a Windows server, and the client needs to open another connection to the same instance that the current connection is using.

+6
Jun 07 '15 at 4:39 on
source share

Why dwell only on the instance name? You can inventory your SQL Server environment as follows:

 SELECT SERVERPROPERTY('ServerName') AS ServerName, SERVERPROPERTY('MachineName') AS MachineName, CASE WHEN SERVERPROPERTY('InstanceName') IS NULL THEN '' ELSE SERVERPROPERTY('InstanceName') END AS InstanceName, '' as Port, --need to update to strip from Servername. Note: Assumes Registered Server is named with Port SUBSTRING ( (SELECT @@VERSION),1, CHARINDEX('-',(SELECT @@VERSION))-1 ) as ProductName, SERVERPROPERTY('ProductVersion') AS ProductVersion, SERVERPROPERTY('ProductLevel') AS ProductLevel, SERVERPROPERTY('ProductMajorVersion') AS ProductMajorVersion, SERVERPROPERTY('ProductMinorVersion') AS ProductMinorVersion, SERVERPROPERTY('ProductBuild') AS ProductBuild, SERVERPROPERTY('Edition') AS Edition, CASE SERVERPROPERTY('EngineEdition') WHEN 1 THEN 'PERSONAL' WHEN 2 THEN 'STANDARD' WHEN 3 THEN 'ENTERPRISE' WHEN 4 THEN 'EXPRESS' WHEN 5 THEN 'SQL DATABASE' WHEN 6 THEN 'SQL DATAWAREHOUSE' END AS EngineEdition, CASE SERVERPROPERTY('IsHadrEnabled') WHEN 0 THEN 'The Always On Availability Groups feature is disabled' WHEN 1 THEN 'The Always On Availability Groups feature is enabled' ELSE 'Not applicable' END AS HadrEnabled, CASE SERVERPROPERTY('HadrManagerStatus') WHEN 0 THEN 'Not started, pending communication' WHEN 1 THEN 'Started and running' WHEN 2 THEN 'Not started and failed' ELSE 'Not applicable' END AS HadrManagerStatus, CASE SERVERPROPERTY('IsSingleUser') WHEN 0 THEN 'No' ELSE 'Yes' END AS InSingleUserMode, CASE SERVERPROPERTY('IsClustered') WHEN 1 THEN 'Clustered' WHEN 0 THEN 'Not Clustered' ELSE 'Not applicable' END AS IsClustered, '' as ServerEnvironment, '' as ServerStatus, '' as Comments 
+3
May 25, '17 at 21:12
source share



All Articles