1

For some reason, I have a table Table_1 in n databases (same name and schema structure) instead of one single databases. The columns are like this:

| Column_1 | Column_2 |

I can select data from those n databases by running this query on each of them:

use Database_n
select * from Table_1

Problem: This will require me to run this query n times. Which will return n result sets as shown in the below image:

n number of result sets

Question: Is there a way to get the data from all those databases in one single result set like this:

one single result set

7

Sure :

    select * from database_n.dbo.Table_1
    UNION ALL
    select * from database_n+1.dbo.Table_1
    UNION ALL
    select * from database_n+2.dbo.Table_1
    -- etc.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.