What is the T-SQL To grant read and write access to tables in a database in SQL Server?

后端 未结 3 838
孤独总比滥情好
孤独总比滥情好 2020-12-14 05:15

What is the exact SQL to assign db_datareader and db_datawriter roles to a user in SQL Server?

The user name is MYUSER and the

相关标签:
3条回答
  • 2020-12-14 05:49

    In SQL Server 2012, 2014:

    USE mydb
    GO
    
    ALTER ROLE db_datareader ADD MEMBER MYUSER
    GO
    ALTER ROLE db_datawriter ADD MEMBER MYUSER
    GO
    

    In SQL Server 2008:

    use mydb
    go
    
    exec sp_addrolemember db_datareader, MYUSER 
    go
    exec sp_addrolemember db_datawriter, MYUSER 
    go
    

    To also assign the ability to execute all Stored Procedures for a Database:

    GRANT EXECUTE TO MYUSER;
    

    To assign the ability to execute specific stored procedures:

    GRANT EXECUTE ON dbo.sp_mystoredprocedure TO MYUSER;
    
    0 讨论(0)
  • 2020-12-14 05:56

    From SQLServer 2012 more elegant alter role:

    use mydb
    go
    
    ALTER ROLE db_datareader
      ADD MEMBER MYUSER 
    go
    ALTER ROLE db_datawriter
      ADD MEMBER MYUSER 
    go
    
    0 讨论(0)
  • 2020-12-14 06:04

    It will be better to Create a New role, then grant execute, select ... etc permissions to this role and finally assign users to this role.

    Create role

    CREATE ROLE [db_SomeExecutor] 
    GO
    

    Grant Permission to this role

    GRANT EXECUTE TO db_SomeExecutor
    GRANT INSERT  TO db_SomeExecutor
    

    to Add users database>security> > roles > databaseroles>Properties > Add ( bottom right ) you can search AD users and add then

    OR

       EXEC sp_addrolemember 'db_SomeExecutor', 'domainName\UserName'
    

    Please refer this post

    0 讨论(0)
提交回复
热议问题