Union 50+ tables with different number of columns using SQL Server
<p>I have over 50 different tables which I would like to combine into one big table.  All the tables have a different number of columns. </p>

<p>Currently, to union the tables together, I am writing an individual select statement for each of the tables, and inserting a null column if that column doesn't exist in the table. Then I am using UNION ALL to union them together. </p>

<p>For example:</p>

<pre><code>(
select col1
        , null as col2 
        , col3
 from   table1

union all

 select col1
       , col2
       , null as col
from   table2
)
</code></pre>

<p>Although this works, it is very manual and time consuming. Is there a better, more efficient way to union these tables into one? As with over 50 tables, I am going to have thousands of lines of code.</p>

<p>Thank you!</p>