I have Setup Always on Availability Groups between 2 servers and i have a Script that i wanted to put in Job and i want to run the job only on primary at any time..(one active and one secondary)USE MASTER GODECLARE @ServerName NVARCHAR(256) = @@SERVERNAME DECLARE @RoleDesc NVARCHAR(60)SELECT @RoleDesc = a.role_desc FROM sys.dm_hadr_availability_replica_states AS a JOIN sys.availability_replicas AS b ON b.replica_id = a.replica_idWHERE b.replica_server_name = @ServerNameIF @RoleDesc = 'PRIMARY'BEGINUSE TestAGInsert into CheckAvailabilityRoleJobs(InsertDate,ServerName,RoleDescription)SELECT GETDATE(),@@SERVERNAME, a.role_descFROM sys.dm_hadr_availability_replica_states AS aJOIN sys.availability_replicas AS b ON b.replica_id = a.replica_idwhere b.replica_server_name=@@SERVERNAMEENDELSE BEGIN RETURN END I wanted to keep this job running 24X7 on both servers, but insert happens only on Primary at any time , in secondary the JOB exits with out actually inserting...the script is running good on PRIMARY, but when i try to run on SECONDARY it is failing ...the script should actually exit on SECONDARY after checking Primary or not but it is trying too connect TESTAG database which is not accessible on secondaryMsg 976, Level 14, State 1, Line 16The target database, 'TestAG', is participating in an availability group and is currently not accessible for queries. Either data movement is suspended or the availability replica is not enabled for read access. To allow read-only access to this and other databases in the availability group, enable read access to one or more secondary availability replicas in the group. For more information, see the ALTER AVAILABILITY GROUP statement in SQL Server Books Online.any help?
↧