1

I have a database with multiple (10) schemas. I need a user to have access to two of them. If I grant access to the database and then GRANT SELECT to 2 schemas, is access to the other schemas DENY-ed by implication? Or must I grant permissions to those schemas and then DENY SELECT to the rest of them?

asked May 31, 2018 at 21:50

1 Answer 1

3

You should create roles and grant access to roles rather than granting users access directly to schema.

Once you assign permissions to the role, you can just add users to the role. This way you dont have to manage permissions for individual users. The users inherit permissions granted to role.

 -- Create the database role
CREATE ROLE TableSelector AUTHORIZATION [dbo]
GO
 ---- Grant access rights to a specific schema in the database
GRANT 
 SELECT, INSERT, UPDATE, DELETE, ALTER 
ON SCHEMA::dbo
 TO TableSelector 
GO
-- Add an existing user to the new role created 
EXEC sp_addrolemember 'TableSelector', 'MyDBUser'
GO
-- Revoke access rights on a schema from a role 
DENY ALTER -- you can customize here ...
ON SCHEMA::dbo
 TO TableSelector 

see my answer to : Setting user permissions for different SQL Server schemas

answered May 31, 2018 at 22:00
4
  • 1
    Note REVOKE and DENY are different. REVOKE removes a GRANT, DENY overrides it. So you can GRANT the whole schema and DENY one table, but not REVOKE it. Commented May 31, 2018 at 22:07
  • As an Accidental DBA I sincerely appreciate this advice, but my base question remains unanswered. Does the "Principle of Least Privilege" apply here, or do I totally misunderstand the concept? Let me rephrase. Using the example above, if I grant SELECT to 'TableSelector' for one schema, is access to the other schema denied? Or do I need to explicitly DENY access to those schema? Commented Jun 1, 2018 at 13:47
  • 1
    If you grant select on one schema then the role wont have access to other schemas. Commented Jun 1, 2018 at 14:08
  • Thanks Kin, I sincerely appreciate this. I will be implementing the roles as you suggest. Commented Jun 1, 2018 at 15:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.