GRANT syntax for domain\user

后端 未结 2 365
盖世英雄少女心
盖世英雄少女心 2021-01-17 07:32

I\'m trying to give access to an active directory user to only one specific table. I want them to be able to insert, update, delete, etc. but only for that table. I know thi

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 08:18

    It is a good practice to create a role and add users to that role. Then grant permissions to that role.

    USE database_name
    GO
    
    --1)create role 
    CREATE ROLE role_name
    GO
    
    --2 create user
    IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'domain\user')
    BEGIN
        CREATE USER [domain\user] FOR LOGIN [domain\user]
    END;
    GO
    
    -- 3 Add user to the role
    ALTER ROLE [role_name] ADD MEMBER [domain\user]
    GO
    
    --4 Grant permissions to the role
    GRANT SELECT, INSERT, UPDATE, DELETE ON dbo.Tablename TO [role_name];
    

提交回复
热议问题