Skip to content
Advertisement

SQL – Select from Multiple databases

I have 10 databases all in the same instance and schema with identical tables.

Looking to create a SELECT query which can use a variable of the database names and return all of the records into a single dataset.

I’ve been looking around and put the following together as a test (the real select statement is much larger with multiple joins)

This test does work but it returns two result sets one for each database.

Is there a way to combine the results into one set or am I using the wrong approach?

Thanks in advance

Advertisement

Answer

I’d create a view which combines the select statements. e.g.

You could include the source database as a column also:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement