views:

147

answers:

1

I have a stored procedure that finds all the existing databases and reads from a table in each one.

Is there a way I can give a login read access to all databases, and to all future databases i.e., I won't have to do anything when a new database is added?

Is there a server role that would work? Is there a way to make a SQL agent job add the permissions on any new databases? Or is there some other method?

+4  A: 

For new databases, add the user in the model database. This is used as the template for all new databases.

USE model
CREATE USER ... FROM LOGIN...
EXEC sp_addrolemember 'db_datareader', '...'

For existing databases, use sp_MSForEachDb

EXEC sp_MSForEachDb '
 USE ?
 CREATE USER ... FROM LOGIN...  
 EXEC sp_addrolemember ''db_datareader'', ''...''
'
gbn
Will the user automatically get read access after running that?
Greg
Well, you'll need to grant some rights too but they'll have DB access
gbn
How do I automate granting read rights to the tables? Otherwise I'm kind of back where I started.
Greg
@greg: I've added permissions too
gbn
Thanks, that's perfect!
Greg