SQL Server 2008 通过角色、架构或脚本的多数据库权限

发布于 2024-12-06 22:18:24 字数 726 浏览 0 评论 0原文

假设我的办公室中有多个用户需要访问多个相互依赖的数据库。我有一些用户是系统管理员,但随着添加更多员工,我希望能够为这些不是系统管理员但需要特定数据库的所有者级别权限的新用户应用特定权限,而不授予他们修改其他用户的权限。我不想只是手动添加每个角色,而是创建一个可以应用于用户的角色,并为每个数据库授予权限,或者创建一个架构,为每个数据库上的用户授予这些权限,或者创建一个脚本来授予用户权限。每个数据库的用户权限。例如,

DB1
 -sysadmin
     (contains user1, user2)
 -role1
     (contains user3, user4)
DB2
 -sysadmin
     (contains user1, user2)
 -role1
     (contains user3, user4)
DB3
 -sysadmin
     (contains user1, user2)
 -role1
     (contains user3, user4)

我想将 user3 和 user4 添加到 role1 一次,他们将拥有所有三个数据库(DB1、DB2、DB3)的权限。根据我的理解,这基本上是服务器级别的角色,但根据我所读到的内容,您不允许创建自定义服务器级别角色。

据我了解,架构是特定于数据库级别的,而不是特定于服务器级别的(我无法将 3 个数据库分组到一个架构中,然后将用户添加到该架构中)。

那么,这是否让我只能编写脚本,或者我是否缺少一种使用角色或模式来执行此操作的方法?

Let's say that there are multiple users in my office that need access to multiple databases that are dependent upon each other. I have users who are sysadmins but as more employees are added I want to be able to apply specific permissions for these new users who are not sysadmins but need owner level permissions on the specific databases without giving them permissions to modify other users. Instead of just adding each one manually I would like to either create a role that can be applied to the user and will give permissions on every database or create a schema that would give these permissions to the user on every database or create a script to give permissions to a user for every database. for example

DB1
 -sysadmin
     (contains user1, user2)
 -role1
     (contains user3, user4)
DB2
 -sysadmin
     (contains user1, user2)
 -role1
     (contains user3, user4)
DB3
 -sysadmin
     (contains user1, user2)
 -role1
     (contains user3, user4)

I want to add user3 and user4 to role1 one time and they will have permissions on all three databases (DB1, DB2, DB3). From my understanding this is basically a server level role, but from what I have read you are not allowed to create custom server level roles.

Also as I understand it schemas are database level specific instead of server level specific (I can't group 3 databases into one schema and then add users to that schema).

So does that leave me with scripting only or am I missing a way to do this with a role or schema?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2024-12-13 22:18:24
DECLARE @username varchar(50) = 'mynewuser'
DECLARE @dbRole varchar(50)
DECLARE @SQL varchar(max)
DECLARE @DatabaseName varchar(MAX)

DECLARE my_cursor CURSOR FOR
    SELECT CAST([Name] AS varchar(MAX)) AS databasename
    FROM sys.sysdatabases
    --only user databases
    WHERE DBID>4 AND [NAME] NOT LIKE '

OPEN my_cursor

WHILE 1=1
BEGIN
    FETCH NEXT FROM my_cursor INTO @DatabaseName
    IF @@FETCH_STATUS <> 0 BEGIN BREAK END

    SET @SQL = '
        USE ' + @DatabaseName + ';
        IF NOT EXISTS(
            SELECT p.name 
            FROM ' + @DatabaseName + '.sys.database_principals p
            WHERE p.name = ''' + @username + '''
        )
        BEGIN
            USE ' + @DatabaseName + ';
            CREATE USER ' + @username + ' FOR LOGIN ' + @username + '
            EXEC sp_addrolemember ''db_owner'', ''' + @username + '''
        END'

    EXECUTE(@SQL)
END

CLOSE my_cursor
DEALLOCATE my_cursor
DECLARE @username varchar(50) = 'mynewuser'
DECLARE @dbRole varchar(50)
DECLARE @SQL varchar(max)
DECLARE @DatabaseName varchar(MAX)

DECLARE my_cursor CURSOR FOR
    SELECT CAST([Name] AS varchar(MAX)) AS databasename
    FROM sys.sysdatabases
    --only user databases
    WHERE DBID>4 AND [NAME] NOT LIKE '

OPEN my_cursor

WHILE 1=1
BEGIN
    FETCH NEXT FROM my_cursor INTO @DatabaseName
    IF @@FETCH_STATUS <> 0 BEGIN BREAK END

    SET @SQL = '
        USE ' + @DatabaseName + ';
        IF NOT EXISTS(
            SELECT p.name 
            FROM ' + @DatabaseName + '.sys.database_principals p
            WHERE p.name = ''' + @username + '''
        )
        BEGIN
            USE ' + @DatabaseName + ';
            CREATE USER ' + @username + ' FOR LOGIN ' + @username + '
            EXEC sp_addrolemember ''db_owner'', ''' + @username + '''
        END'

    EXECUTE(@SQL)
END

CLOSE my_cursor
DEALLOCATE my_cursor
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文