Skip to content
Advertisement

Displaying multiple database tables in one table

I have multiple databases with the same table (an Eventlog with different values). The names of these databases are subject to change. I am trying to display the Eventlog tables in one consolidated table with the corresponding database name.

I tried to using cursor and dynamic SQL statement to achieve this with no luck. As well, I’m not sure if that is the best approach. Would love some help!

Advertisement

Answer

If you need a single resultset and all tables have the same layout, this should work:

If you’re on compatibility level 130 or lower, you will have to use XML PATH(TYPE, '') to aggregate. I will leave that to you.

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