Hi @Sravya Pullagura ,
DB on secondary node is in read only mode, so you will fail,
Try to use the sys.fn_hadr_is_primary_replica
, it can be used to determine if the current replica is the primary replica.
You could have the jobs on both instances, primary and secondary replica and then use sys.fn_hadr_is_primary_replica
in the first step of your job.
IF sys.fn_hadr_is_primary_replica ( 'yourDBname' ) <> 1
BEGIN
-- raiserror, so the job step fails and the entire job fails
-- sometimes you may want to set this job to "finish with success" when this step fails
-- so that you don't get alerts
declare @errMsg varchar(600) = 'This is meant to run on the primary replica'
raiserror(@errMsg,16,1)
END
ELSE
BEGIN
print 'This is the primary replica, continue with the job'
END
See this thread and this for detail
-------------
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.