Add DB Role IF NOT EXISTS

Check if the Database Role exists before creating it:

DECLARE @Role varchar(20) = 'UserRole'
IF NOT EXISTS (SELECT name FROM sysusers
WHERE issqlrole=1 AND name = @Role)

EXEC sp_addrole @Role; --EXEC sp_addrole @Role, 'dbo'

No comments: