Querying from two different schema tables

Viewed 241

I have two tables - table1 and table2 - with the same columns in two different schema.

Table1

col1 | col2 | col3
-----+------+------
a1   | b1   | c1
a2   | b2   | c2

and table2

col1 | col2 | col3
-----+------+------
a1   | b1   | c1
a2   | b2   | c2

How to query from both tables (schema1.table1 and schema2.table2) so that I get the result as:

  col1 | col2 | col3
  -----+------+------
   a1  | b1   | c1
   a2  | b2   | c2
   a1  | b1   | c1
   a2  | b2   | c2
3 Answers

This looks like a simple union

select col1, col2, col3
from schema_1.table1
union all
select col1, col2, col3
from schema_2.table2

Just use union all and get the data from those two tables

SELECT col1, col2, col3 FROM schema1.dbo.table1
UNION ALL
SELECT col1, col2, col3 FROM schema2.dbo.table2
Related