I’ve got a software suite that is based off of multiple libraries where:
1 library = 1 SQL Database.
Different users can have different access to different libraries.
In addition, the databases are named in a specific manner to help identify which are “mine” and which aren’t.
I’d like to create a stored procedure that takes a variable called @UserName and returns the databases that have a name starting with MYDB, where @UserName is found in a table USERS.
I’m figuring that I’ll start with EXEC sp_databases, but I’m unsure how to continue.
What I need to know is:
- How do I iterate the results of sp_databases to pull out just the databases that have a name matching my pattern?
- How do I then check for
@UserNamein the[USER NAME]column of theUSERStable of each database returned from #1?
I’m guessing it has something to do with temp tables and cursors, but I’m not really sure where to start.
Any help?
Thanks!
Here is some proof of concept code to show you an approach. sys.databases contains a more accessible list of databases. You’ll pretty much have to use dynamic sql at some point though.