I have a user QATest and I want to build a script to add said users to have db_datareader access. Is there a way to write it to give it access to all db's as a reader without writing it out 15 times?
use test
if exists (select * from sysusers where name = 'QATest' and issqluser = 1)
begin
PRINT 'QATest already exists in test database.'
exec sp_change_users_login 'Auto_Fix', 'QATest'
end
else
begin
exec sp_grantdbaccess 'QATest'
exec sp_addrolemember 'db_datareader', 'QATest'
end
go
use test
if exists (select * from sysusers where name = 'QATest' and issqluser = 1)
begin
PRINT 'QATest already exists in test database.'
exec sp_change_users_login 'Auto_Fix', 'QATest'
end
else
begin
exec sp_grantdbaccess 'QATest'
exec sp_addrolemember 'db_datareader', 'QATest'
end
go