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

Advertisement

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 )

Facebook photo

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

Connecting to %s