SQL Server - How to Grant Read Access to ALL databases to a Login?

后端 未结 7 732
独厮守ぢ
独厮守ぢ 2021-02-05 05:22

I need to give a new login read access to all 300 databases on a server. How can I accomplish this without checking 300 checkboxes in the user mapping area?

7条回答
  •  佛祖请我去吃肉
    2021-02-05 05:51

    EXEC sp_MSForEachDB 
    'Declare @name varchar(100)
     select @name = ''?''
     PRINT @name
     IF db_id(@name) > 4
     BEGIN
     USE ?
     CREATE USER [user] FOR LOGIN [user];
    EXEC sp_addrolemember ''db_datareader'', ''user''
     END'
    

提交回复
热议问题