Query to get Orphaned users across all the databases
use master GO --CREATE table master..TBL_ORPHAN_USERS(dbname varchar(300),username varchar(300),UserSID VARBINARY(90)) GO declare @dbname varchar(90) declare cur cursor for SELECT name FROM sys.databases WHERE name NOT IN ('master','model','msdb','tempdb') open cur fetch next from cur into @dbname while @@FETCH_STATUS = 0 begin exec (' USE '+@dbname+' INSERT INTO master..TBL_ORPHAN_USERS select dbname = DB_NAME() ,UserName = name, UserSID = sid from sysusers where issqluser = 1 and (sid is not null and sid <> 0x0) and (len(sid) <= 16) and suser_sname(sid) is null order by name ') fetch next from cur into @dbname end close cur deallocate cur GO SELECT * FROM master..TBL_ORPHAN_USERS |
No comments:
Post a Comment