I’m trying to create a little SQL script (in SQL Server Management Studio) to get a list of all tables in two different databases. The goal is to find out which tables exist in both databases and which ones only exist in one of them.
I have found various scripts on SO to list all the tables of one database, but so far I wasn’t able to get a list of tables of multiple databases.
So: is there a way to query SQL Server for all tables in a specific database, e.g. SELECT * FROM ... WHERE databaseName='first_db' so that I can join this with the result for another database?
UPDATE
In order to compare the two lists, you can use
FULL OUTER JOIN, which will show you the tables that are present in both databases as well as those that are only present in one of them:You can also add
WHERE db1.TABLE_NAME IS NULL OR db2.TABLE_NAME IS NULLto see only the differences between the databases.