A database-level Login can be associated with zero to one server-instance-level Logins. And if it exists, that in turn can be associated with zero to one database-level Logins on another database.
Is it possible to retrieve the matching Login using a single query?
Given the answer to this question, I suspect it’s not. But it’s worth asking.
Assuming you have a local database user named
foo, you can use this query to find out if there is a related user in database [splunge]:If you don’t know which other database(s) the related login may be found in, then no, there isn’t a simple way without constructing a query like the answer in the other question you pointed to. If you are going to use sp_msForEachDB, please use caution:
One way to do this a little easier would be:
This doesn’t meet your requirement of "a single query" but maybe you could explain why that is a requirement.