Skip to content
Advertisement

USE and UNION ALL

I am working with MS SQL Server and I am trying to gather informations from a few different databases and I need the results in one table.

USE [1st Database]

   select smthing

USE [2nd Database]

   select smthing

USE [3rd Database]

And UNION ALL somewhere between. I cannot find any solution right now.

Advertisement

Answer

You can reference the database name along with table :

select t.col1, t.col2, . . . 
from <db-name>.<schema>.<tablename> t union all
select t.col1, t.col2, . . . 
from <db-name>.<schema>.<tablename> t;

This needs to access to the all databases you included in the select statement.

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