create custom role equivalent to sysadmin

Heisenberg 246 Reputation points
2023-01-20T21:15:32.0233333+00:00

hi Folks,

I have a weird requirement. Is it possible to create a custom role that has all the permissions like sysadmin ? Basically i want to create a custom role like sysadmin but by excluding certain permissions like "alter database" "control" permission etc.

Thank you

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Santhi Swaroop Naik Bukke 595 Reputation points
    2023-01-20T21:18:59.2233333+00:00

    Yes, it is possible to create a custom role in SQL Server that has similar permissions to the sysadmin role, but with certain permissions excluded.

    Here is an example of how you can create a custom role with all sysadmin permissions except "ALTER DATABASE" and "CONTROL" permissions:

    Copy code
    CREATE ROLE custom_sysadmin 
    GO
    
    -- Grant all sysadmin permissions to custom_sysadmin role
    EXEC sp_addsrvrolemember 'custom_sysadmin', 'sysadmin'
    GO
    
    -- Revoke ALTER DATABASE and CONTROL permissions from custom_sysadmin role
    REVOKE ALTER DATABASE TO custom_sysadmin
    REVOKE CONTROL TO custom_sysadmin
    GO
    

    This code creates a new role called custom_sysadmin and grants all permissions of the sysadmin role to it. Then it revokes "ALTER DATABASE" and "CONTROL" permissions from the custom_sysadmin role.

    Note that you can also use the GRANT statement to grant specific permissions to custom_sysadmin role, you can use DENY statement to deny specific permissions to custom_sysadmin role.

    You can also use the sp_helprotect stored procedure to check the permissions of the role.

    Please be aware that creating a custom role with sysadmin permissions can create a security risk, and should be used with caution. It is highly recommended to test the custom role in a non-production environment before deploying to production.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.