1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
exec sp_MSforeachdb 'Use ? if not exists ( SELECT 1 FROM sys.database_role_members RoleMem JOIN sys.database_principals RolePri ON RoleMem.role_principal_id = RolePri.principal_id JOIN sys.database_principals MemPri ON RoleMem.member_principal_id = MemPri.principal_id where Mempri.name=''riteshshah'') and ''?'' like ''ad%'' select ''?'' ' --My database is starting with AD so ad%..... you can have your own pattern.