I have this problem. There are n tables created dynamically and each table has m columns, the columns could be repeated. This tables have in common 2 columns but there is no related data between them, for example: Table1 | A | B | Col1 | Col2 |
Table2
| A | B | Col3 | Col4 |
Table3
| A | B | Col1 | Col2 | Col4 |
What I want to do is to merge all the tables into a big one like this:
BigTable
| A | B | Col1 | Col2 | Col3 | Col4 |
And all the rows concatenated, for example if in table1 rows = 5, table2 rows = 3, table3 rows = 2, the big table will have 10 entries.
I can accomplish this by using a query like this:
SELECT A, B, Col1, Col2, null as Col3, null as Col4 FROM Table1
UNION
SELECT A, B, null as Col1, null as Col2, Col3, Col4 FROM Table2
UNION
SELECT A, B, Col1, Col2, null as Col3, Col4 FROM Table3
But I want to know if there is a better way to do this, because there will be more columns and more tables, and there is the possibility that all the columns are different.