DECLARE @SQLServerMajorBuildVersion AS INT
SELECT @SQLServerMajorBuildVersion =
LEFT(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR),
CHARINDEX('.',CAST(SERVERPROPERTY('ProductVersion') AS
VARCHAR))-1)
IF @SQLServerMajorBuildVersion < 11
BEGIN
IF (SELECT
SERVERPROPERTY('IsClustered')) = 1
BEGIN
SELECT
UPPER(CAST(SERVERPROPERTY('ServerName') AS VARCHAR)) AS
InstanceName
,UPPER(NodeName) AS ServerName
,SERVERPROPERTY('IsClustered') AS IsClustered
,SERVERPROPERTY ('IsHadrEnabled') AS
IsAvailabilityGroupEnabled
,NULL AS
AvailabilityGroupName
,NULL AS
AvailabilityGroupListenerName
,CASE
UPPER(NodeName)
WHEN
UPPER(CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS
VARCHAR)) THEN 'PRIMARY'
ELSE 'SECONDARY'
END AS
HighAvailabilityRoleDesc
,'FCI' AS
SQLServerClusteringMethod
,@@VERSION AS
SQLServerVersion
FROM
sys.dm_os_cluster_nodes
ORDER BY
NodeName;
END
ELSE
BEGIN
SELECT
UPPER(CAST(SERVERPROPERTY('ServerName') AS VARCHAR)) AS
InstanceName
,UPPER(CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS
VARCHAR)) AS ServerName
,SERVERPROPERTY('IsClustered') AS IsClustered
,SERVERPROPERTY ('IsHadrEnabled') AS
IsAvailabilityGroupEnabled
,NULL AS
AvailabilityGroupName
,NULL AS
AvailabilityGroupListenerName
,NULL AS
HighAvailabilityRoleDesc
,'Standalone'
AS SQLServerClusteringMethod
,@@VERSION AS
SQLServerVersion
END
END
ELSE
BEGIN
IF (SELECT
SERVERPROPERTY('IsHadrEnabled')) = 1 AND (SELECT
SERVERPROPERTY('IsClustered')) = 1
BEGIN
SELECT
UPPER(CAST(SERVERPROPERTY('ServerName') AS VARCHAR)) AS
InstanceName
,UPPER(ar.replica_server_name) AS ServerName
,SERVERPROPERTY('IsClustered') AS IsClustered
,SERVERPROPERTY ('IsHadrEnabled') AS
IsAvailabilityGroupEnabled
,ag.name AS
AvailabilityGroupName
,agl.dns_name
AS AvailabilityGroupListenerName
,CASE ar.replica_server_name WHEN
dhags.primary_replica THEN 'PRIMARY'
ELSE 'SECONDARY'
END AS
HighAvailabilityRoleDesc
,'FCI/AG' AS
SQLServerClusteringMethod
,@@VERSION AS
SQLServerVersion
FROM
sys.availability_groups AS ag
LEFT OUTER
JOIN sys.availability_replicas AS ar ON ag.group_id =
ar.group_id
LEFT OUTER
JOIN sys.availability_group_listeners AS agl ON ag.group_id =
agl.group_id
LEFT OUTER JOIN
sys.dm_hadr_availability_group_states as dhags ON ag.group_id =
dhags.group_id
WHERE
UPPER(CAST(SERVERPROPERTY('ServerName') AS VARCHAR)) <>
ar.replica_server_name
UNION
ALL
SELECT
UPPER(CAST(SERVERPROPERTY('ServerName') AS VARCHAR)) AS
InstanceName
,UPPER(NodeName) AS ServerName
,SERVERPROPERTY('IsClustered') AS IsClustered
,SERVERPROPERTY ('IsHadrEnabled') AS
IsAvailabilityGroupEnabled
,NULL AS
AvailabilityGroupName
,NULL AS
AvailabilityGroupListenerName
,CASE
is_current_owner
WHEN 1 THEN 'PRIMARY'
ELSE 'SECONDARY'
END AS
HighAvailabilityRoleDesc
,'FCI/AG' AS
SQLServerClusteringMethod
,@@VERSION AS
SQLServerVersion
FROM
sys.dm_os_cluster_nodes
ORDER BY
5,2;
END
ELSE
BEGIN
IF (SELECT
SERVERPROPERTY('IsHadrEnabled')) = 1
BEGIN
SELECT
UPPER(CAST(SERVERPROPERTY('ServerName') AS VARCHAR)) AS
InstanceName
,UPPER(ar.replica_server_name) AS ServerName
,SERVERPROPERTY('IsClustered') AS IsClustered
,SERVERPROPERTY ('IsHadrEnabled') AS
IsAvailabilityGroupEnabled
,ag.name
AS AvailabilityGroupName
,agl.dns_name AS AvailabilityGroupListenerName
,CASE
ar.replica_server_name WHEN dhags.primary_replica THEN
'PRIMARY'
ELSE
'SECONDARY'
END AS
HighAvailabilityRoleDesc
,'AG' AS
SQLServerClusteringMethod
,@@VERSION
AS SQLServerVersion
FROM
sys.availability_groups AS ag
LEFT OUTER
JOIN sys.availability_replicas AS ar ON ag.group_id =
ar.group_id
LEFT OUTER
JOIN sys.availability_group_listeners AS agl ON ag.group_id =
agl.group_id
LEFT OUTER
JOIN sys.dm_hadr_availability_group_states as dhags ON
ag.group_id = dhags.group_id
ORDER BY
5,2;
END
ELSE
BEGIN
IF (SELECT SERVERPROPERTY('IsClustered')) = 1
BEGIN
SELECT
UPPER(CAST(SERVERPROPERTY('ServerName') AS VARCHAR)) AS
InstanceName
,UPPER(NodeName) AS
ServerName
,SERVERPROPERTY('IsClustered')
AS IsClustered
,SERVERPROPERTY
('IsHadrEnabled') AS IsAvailabilityGroupEnabled
,NULL AS
AvailabilityGroupName
,NULL AS
AvailabilityGroupListenerName
,CASE
is_current_owner
WHEN 1
THEN 'PRIMARY'
ELSE
'SECONDARY'
END AS
HighAvailabilityRoleDesc
,'FCI' AS
SQLServerClusteringMethod
,@@VERSION AS
SQLServerVersion
FROM
sys.dm_os_cluster_nodes
ORDER BY
NodeName;
END
ELSE
BEGIN
SELECT
UPPER(CAST(SERVERPROPERTY('ServerName') AS VARCHAR)) AS
InstanceName
,UPPER(CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS')
AS VARCHAR)) AS ServerName
,SERVERPROPERTY('IsClustered')
AS IsClustered
,SERVERPROPERTY
('IsHadrEnabled') AS IsAvailabilityGroupEnabled
,NULL AS
AvailabilityGroupName
,NULL AS
AvailabilityGroupListenerName
,NULL AS
HighAvailabilityRoleDesc
,'Standalone' AS
SQLServerClusteringMethod
,@@VERSION AS
SQLServerVersion;
END
END
END
END;