2014-03-04 31 views
1

我想創建一個查詢,列出允許特定用戶讀取和寫入的所有數據庫名稱。查找數據庫,其中特定用戶是db_owner和db_datawriter

我能夠找到如何列出所有的數據庫名稱,但我不知道該如何檢查上述條件。

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

This [link](http://invalidlogic.com/2007/03/04/sql-server-tip-how-to-find-the-owner-of-a-database-through-t-sql/ ) 可能有幫助。如果確實如此,您可以將其作爲答案發布(經過一段時間後)。 –

回答

0

這個代碼將會給你找到主人的owner_sid和名稱上的SQL Server上的所有數據庫

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權限的用戶數據庫的隱式訪問?

+0

它不是真的我需要我給你一個例子:我有3個數據庫:第一,第二和第三。我的用戶是Alex和John。允許Alex在第一和第二數據庫中寫入和讀取,並允許John在第二和第三數據庫中寫入和讀取。我現在想要的是要知道允許Alex在哪些數據庫中編寫和讀取,並且我需要數據庫的名稱作爲查詢的結果 – user3379507

相關問題