Con questa query T-SQL è possibile determinare se la stiamo eseguendo sul nodo primario di un AlwaysOn High Availability group di Sql Server 2012:

SQL

SELECT AGC.name, RCS.replica_server_name, ARS.role_desc
   Availability Group, SQL cluster node name, Replica Role
FROM sys.availability_groups_cluster AS AGC
  INNER JOIN sys.dm_hadr_availability_replica_cluster_states AS RCS
    ON RCS.group_id = AGC.group_id
  INNER JOIN sys.dm_hadr_availability_replica_states AS ARS
    ON ARS.replica_id = RCS.replica_id
WHERE ARS.role_desc = 'PRIMARY' AND replica_server_name = SERVERPROPERTY('MachineName')
se ritorna dei risultati siamo sul nodo primario.

Può essere usata in un job per cambiare il comportamento quando siamo o meno sul nodo primario:

SQL

IF EXISTS(
  SELECT AGC.name, RCS.replica_server_name, ARS.role_desc
    -- Availability Group, SQL cluster node name, Replica Role
  FROM sys.availability_groups_cluster AS AGC
    INNER JOIN sys.dm_hadr_availability_replica_cluster_states AS RCS
      ON RCS.group_id = AGC.group_id
    INNER JOIN sys.dm_hadr_availability_replica_states AS ARS
      ON ARS.replica_id = RCS.replica_id
  WHERE ARS.role_desc = 'PRIMARY' AND replica_server_name = SERVERPROPERTY('MachineName')
)
BEGIN
  SELECT 'Is Primary..'
END
ELSE
BEGIN
  SELECT 'Is secondary..'
END
Tags:
SQL90 SQL Server100 SQL Server 20125 T-SQL66
Potrebbe interessarti anche: