Below script can be used to get a list of databases and their owner details for a current SQL Server instance. The quickest and east way to get those details is use sys.databases systems view with SUSER_SNAME () inbuilt security function. Also xp_logininfo systems stored procedure can be included to the script to get more information about Windows users and Windows groups.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
USE [master]; GO DECLARE @UserList TABLE ( [AccountName] NVARCHAR(128), [Type] NVARCHAR(64), [Privilege] NVARCHAR(64), [Mapped Login Name] NVARCHAR(128), [Permission Path] NVARCHAR(MAX) ) INSERT INTO @UserList EXEC xp_logininfo SELECT @@SERVERNAME AS [Server Name] , d.name AS [Database Name], state_desc as [Status], suser_sname(d.owner_sid) AS [Database Owner], u.[Type], u.[Privilege], u.[Mapped Login Name] FROM sys.databases d LEFT JOIN @UserList u ON suser_sname(d.owner_sid)=u.[AccountName] ORDER BY d.name |
Leave a Comment