SQL Server - 授予对所有现有和未来数据库的读取访问权限的登录权限 [英] SQL Server - Give a Login Permission for Read Access to All Existing and Future Databases

查看:61
本文介绍了SQL Server - 授予对所有现有和未来数据库的读取访问权限的登录权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,可以找到所有现有的数据库并从每个数据库中的一个表中读取.

I have a stored procedure that finds all the existing databases and reads from a table in each one.

有没有一种方法可以为所有数据库以及所有未来的数据库提供登录读取访问权限,即在添加新数据库时我无需执行任何操作?

Is there a way I can give a login read access to all databases, and to all future databases i.e., I won't have to do anything when a new database is added?

是否有可用的服务器角色?有没有办法让 SQL 代理作业添加对任何新数据库的权限?或者有其他方法吗?

Is there a server role that would work? Is there a way to make a SQL agent job add the permissions on any new databases? Or is there some other method?

推荐答案

对于新数据库,在模型数据库中添加用户.这被用作所有新数据库的模板.

For new databases, add the user in the model database. This is used as the template for all new databases.

USE model
CREATE USER ... FROM LOGIN...
EXEC sp_addrolemember 'db_datareader', '...'

对于现有数据库,使用 sp_MSForEachDb

For existing databases, use sp_MSForEachDb

EXEC sp_MSForEachDb '
 USE ?
 CREATE USER ... FROM LOGIN...  
 EXEC sp_addrolemember ''db_datareader'', ''...''
'

这篇关于SQL Server - 授予对所有现有和未来数据库的读取访问权限的登录权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆