查找特定用户为db_owner和db_datawriter的数据库

时间:2014-03-04 14:56:22

标签: sql sql-server database

我想创建一个查询,列出允许特定用户读写的所有数据库名称。

我能够找到如何列出所有数据库名称,但我不知道要检查上述条件。

SELECT name
FROM master.sys.databases

如何检查是否允许特定用户在数据库中读写?

这是我想要的例子:

I have 3 database : First,Second and Third. my users are Alex and John. 
Alex is allowed to write and read in the First and Second database 
and John is allowed to write and read in the Second and the Third database.
What i want now, is to know in which databases Alex is allowed to write and read
and I need the name of the databases as result of the query 

1 个答案:

答案 0 :(得分:0)

此代码将使您能够在SQL Server上的所有数据库中查找owner_sid和所有者名称

SELECT *, owner_sid,suser_sname(owner_sid) as 'owner'
FROM master.sys.databases

然后获取您感兴趣的人的owner_sid,然后使用where子句作为示例运行。

SELECT *, owner_sid,suser_sname(owner_sid) as 'owner'
FROM master.sys.databases
where owner_sid = 0x010500000000000515000000C34F142B1B2BCD5BE0276300E3B32200

db_datawriter角色允许对数据库中的表和视图进行隐式访问,因此我不确定是否要将具有该用户的表的数据库恢复为db_datawriter?