Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

if role exists sql

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
Source by dba.stackexchange.com #
 
PREVIOUS NEXT
Tagged: #role #exists #sql
ADD COMMENT
Topic
Name
1+2 =