SQL Server Job syntax

Job ID: 35188171

Budget: $10 – $30 USD

I am attempting to get the following code to work in a SQL Server 2019 Standard job for basic availability groups but it is failing with the below error and I don't know why.

IF (SELECT primary_replica FROM sys.dm_hadr_availability_group_states) is NULL
BEGIN
PRINT 'This is a not an AG member server. –Not an AG Member Server and Job Execution should continue.'
END
ELSE IF (SELECT primary_replica FROM sys.dm_hadr_availability_group_states) = @@Servername
BEGIN
PRINT 'This is a AG Primary Replica. –Do Nothing. This is an AG Primary Replica and Job Execution should continue.'
END
ELSE IF (Select primary_replica FROM sys.dm_hadr_availability_group_states)= @@Servername
BEGIN
PRINT 'This is a AG Secondary Replica. –Stop Job. This is an AG Secondary Replica and we do not want Job Execution to continue on this Node.'
DECLARE @job_id UNIQUEIDENTIFIER;
SET @job_id = CONVERT(UNIQUEIDENTIFIER, CONVERT(VARBINARY(34), '$(ESCAPE_SQUOTE(JOBID))', 1));
EXECUTE msdb.dbo.sp_stop_job @job_id = @job_id;
END


Error:
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Msg 512, Level 16, State 1, Line 5
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Msg 512, Level 16, State 1, Line 9
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.



Code in comments section on this website: https://blog.waynesheffield.com/wayne/archive/2018/04/primary-replica-jobs/
Comment #4