51

I'm very green when it comes to the world of database permissions management in SQL Sever.

Let's keep an example simple.

Say account 'admin' is the owner of schemas A, B, and C.

There another account 'minion' that you want to have full rights (update/delete/insert/select/alter) on any object (table/view) created under schemas A, B, and C.

Is this possible? Or do you have to execute a grant statement each and every time you add a table/ view under these schemas? (seems a bit silly to me).

2 Answers 2

73

You can GRANT schema permissions that are effective for everything existing and everything that will exist in that schema.

Grant Schema Permissions

GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA :: <schema> TO <user>;

Further to that, if you want to then deny permissions on a certain object within that schema, you can do.

Denying Object Permissions

DENY INSERT ON OBJECT::<schema>.<object> TO <user>;
2
  • 1
    Also use square brackets if the schema or username has special characters. e.g .... SCHEMA :: [schema] TO [company-name/joshua.duxbury] Commented Apr 18, 2018 at 10:17
  • Doesn't work in Azure, any idea why? (the commands work, but SSMS comes up and says "you are not the Db owner.. blah blah blah. You might not be able to save the table" and sure enough, you can't (did a Grant ALL to the database (shows permissions 'Database', '<db name>', TheUserId, 'Create Table', and state_desc of 'GRANT') Is there some other magic that is needed? Or is Azure only usable by the Dbo? Commented Aug 26, 2018 at 0:06
22

To simplify a bit further, you can use roles to do the job that you are looking for.

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.

Below is an example to get you started :

 -- 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 

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.