Does anyone know how to determine the active node of a SQL Active-Passive Failover Cluster programmatically from T-SQL?
@@SERVERNAME
only returns the virtual server name, which is identical from both nodes.
I don't plan to make any decisions based on the data - I trust the failover to do its thing - but I would like to include the information in an event log so I can tell which node in the cluster was active when the event occurred, or help determine if exceptions come up as a result of a failover.
Select ServerProperty('ComputerNamePhysicalNetBIOS')
This works in newer versions:
SELECT * FROM fn_virtualservernodes();
full details at https://docs.microsoft.com/en-us/sql/relational-databases/system-functions/sys-fn-virtualservernodes-transact-sql?view=sql-server-2017
SELECT * FROM sys.dm_os_cluster_nodes;
Try this, this checks if Server is the Primary server in the Availability Group and then based on this condition do X:
IF EXISTS (SELECT ars.role FROM sys.dm_hadr_availability_replica_states ars JOIN sys.availability_groups ag ON ars.group_id = ag.group_id WHERE ars.role_desc = 'PRIMARY')
BEGIN
SELECT 'PRIMARY' -- DO STUFF IF PRIMARY
END
ELSE
BEGIN
SELECT 'NOT PRIMARY' --- DON'T DO STUFF
END
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.