Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 database is MYDB.

like image 640
ConsultUtah Avatar asked Jan 19 '09 02:01

ConsultUtah


People also ask

How do you grant read and write access to a table in SQL Server?

Expand Security - Logins, then Right Click on login and then click Properties. Go to User Mapping tab and select the database on which you want to give permission and at bottom select db_datareader as shown below. Click Ok and you're done.


2 Answers

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; 
like image 172
cmsjr Avatar answered Sep 28 '22 03:09

cmsjr


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 
like image 40
dani herrera Avatar answered Sep 28 '22 02:09

dani herrera