This is probably really obvious and I might just need a fresh pair of eyes to check it, but I have this SQL Agent job that I need to run only against the Primary replica in an AlwaysOn AG setup. It works perfectly when I run the script in Management Studio, but I get different results when I execute it as a job. [code="sql"]use MyDBgoif (select ars.role_desc from sys.dm_hadr_availability_replica_states ars inner join sys.availability_groups ag on ars.group_id = ag.group_id where ag.name = 'OBFUSCATED_AG' and ars.is_local = 1) = 'PRIMARY'begin EXECUTE my_stored_procedureendelseprint 'This is not the primary replica.'[/code]When I run that as a script on the primary replica, it succeeds:[code="sql"]Command(s) completed successfully.[/code]When I run it as a script on the secondary replica, it succeeds:[code="sql"]This is not the primary replica.[/code]But when I put that script into a SQL Agent job step and execute it on the primary replica, the job reports success and outputs[code="sql"]Executed as user: [Obfuscated\User]. This is not the primary replica[/code]But it is the primary replica. What am I missing? I've checked all the brackets :-D. There are no other job steps.Thanks
↧