Readonly permissions to all Databses on SQL servers

Hi Readers,

There was a requirement for application for readonly access to all databases  on sa SQL server.

Also, any new databses should be created with readonly access to that user. (Thats why this user should be included in model database)

Here is the code to achieve it.

——————————————————————————————————————————

EXEC sp_MSforeachdb ‘
IF ”?” NOT IN(”master”,”tempdb”,”msdb”)
BEGIN
    USE [?];
 PRINT ”Processing ” + DB_NAME();
 CREATE USER [Domain\User];
 EXEC sp_addrolemember ”db_datareader”, ”Domain\User”;
END’;

———————————————————————————————————————————————-

Regards

Vikas

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s