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

后端 未结 7 709
独厮守ぢ
独厮守ぢ 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:46

    Cursor through the databases and GRANT access to each with a little t-sql.

    I did not test the code below.

    DECLARE db_cursor CURSOR FOR
    SELECT name 
    FROM master.dbo.sysdatabases 
    WHERE name NOT IN ('master','model','msdb','tempdb') 
    
    
    WHILE @@FETCH_STATUS = 0  
    BEGIN  
    
    GRANT SELECT ON DATABASE::@name to 'username'; 
    
    FETCH NEXT FROM db_cursor INTO @name  
    END 
    

提交回复
热议问题