Writing SQL to check for a role if exist and add member I am not to good at writing SQL so please help with below as i am doing something wrong here

I need to alter a role say role_a to add member Domain/SQLAgent

The problem is how can i check or write a query to do this on SQL servers where there is one more role role_b only existing if role a is not there

Basically, how can i write a script to check on server if role a exist alter that role to add member and if role b then do same. 

modifying as Q is not duplicate

I want to check if role_a exist then

ALTER ROLE [role_a] ADD MEMBER [Domain/SqlAgent]


else

ALTER ROLE [role_b] ADD MEMBER [Domain/SqlAgent]


Note@ Both the roles do no exist together
Your basic question boils down to "How can I tell if a given role exists in a database." All of the other code you already know how to do.

The easiest way is to query the sys.database_principals table to see if the role is there. Based on your sample question, here is a good check and update into the different roles:

IF EXISTS (
    SELECT * 
    FROM sys.database_principals 
    WHERE name = 'role_a' 
        and type = 'R'
        )
BEGIN   -- Role_A exists, add to role
    ALTER ROLE [role_a] ADD MEMBER [Domain/SqlAgent]
END
ELSE IF EXISTS (
    SELECT * 
    FROM sys.database_principals 
    WHERE name = 'role_b' 
        and type = 'R'
        )
BEGIN   -- Role_B exists, add to role
    ALTER ROLE [role_b] ADD MEMBER [Domain/SqlAgent]
END