Quantcast
Channel: SQL Server High Availability and Disaster Recovery forum
Viewing all articles
Browse latest Browse all 4689

Switching Back From the Secondary Back to the Primary Node

$
0
0

Hello,

We have 2 node SQL2k12 x64 AlwaysOn cluster (ServerA and ServerB. ServerA is the primary and should be the primary at any given point of time unless there is a fail-over to the secondary). Due to some networking issues (which is being looked at), the fail-over from the primary to the secondary happens quite frequently. No data loss so far. Would like to create a script which checks the current status of the primary node and see whether it is currently the primary or the secondary node. If it is secondary node (replica_server_name in the below shown script is other than ServerA, it should execute the script to fail-over back to the primary node after certain time interval (say after 30 min). Below script will show the name of the current primary node (replica_server_name). So, basically, I think there is need for a IF, THEN ELSE statement included. Can you please help on this?

IF SERVERPROPERTY ('IsHadrEnabled') = 1
BEGIN
SELECT
   AGC.name -- Availability Group
 , RCS.replica_server_name -- SQL cluster node name
 , ARS.role_desc  -- Replica Role
 , AGL.dns_name  -- Listener Name
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
  INNER JOIN sys.availability_group_listeners AS AGL
   ON
    AGL.group_id = ARS.group_id
WHERE
 ARS.role_desc = 'PRIMARY'
 order by rcs.replica_server_name
END

Thanks.

Victor


Victor


Viewing all articles
Browse latest Browse all 4689

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>