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

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

    I just needed a user that will have access to all database with a data reader permission so i used this code: you will need to run the result from the query btw.

    USE [master]
    GO CREATE LOGIN [DOMAIN\USER] FROM WINDOWS WITH DEFAULT_DATABASE=[master] GO
    select 'use ['+name+']
    CREATE USER [DOMAIN\USER] FOR LOGIN [DOMAIN\USER]
    EXEC sp_addrolemember N''db_datareader'', N''DOMAIN\USER''
    '
    from sys.databases
    

    if you don't want to apply it on system databases just add where database_id > 6

    0 讨论(0)
提交回复
热议问题