These are queries to manage SQL Server AlwaysOn Availability Groups Using sys.fn_hadr_is_primary_replica to identify if the database is primary replica SELECT sys.fn_hadr_is_primary_replica ('my_ag_db'); Display All availability groups visible to this server when the Server is the Primary replica SELECT Groups.[Name] AS AGname FROM sys.dm_hadr_availability_group_states States INNER JOIN master.sys.availability_groups Groups ON States.group_id = Groups.group_id WHERE primary_replica = @@Servername; Display All availability groups visible to this server when this Server is a Secondary replica SELECT Groups.[Name] AS AGname FROM sys.dm_hadr_availability_group_states States INNER JOIN master.sys.availability_groups Groups ON States.group_id = Groups.group_id WHERE primary_replica != @@Servername; Display All Databases Across all Availability groups within the... Read more →