4

有没有办法在所有数据库上授予用户相同的角色?服务器级角色仅向用户授予服务器特定选项。我想授予用户对服务器上所有数据库的只读访问权限

4

1 回答 1

1

sp_MSforeachdb可能会为您解决问题:

EXECUTE master.sys.sp_MSforeachdb '
    use [?];
    declare @user sysname = ''MyUserName''
    if db_name() not in (''master'', ''model'', ''msdb'', ''tempdb'')
    begin
        if not exists (
            select null
            from dbo.sysusers
            where name = @user
        )
        begin
            print ''Adding '' + @user + '' to '' + db_name();
            exec sp_grantdbaccess @user, @user;
        end;
    print ''Granting '' + @user + '' read access to '' + db_name();
    exec sp_addrolemember db_datareader, @user;
    end;
'
于 2012-07-26T13:15:40.797 回答