Tuesday, October 30, 2012

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

No comments:

Post a Comment