Sql-server – SQL view, how to append results from tableB with the results from tableA

sql serverview

I am using SQL server 2012. I would like to generate a sql view where I got all records from tableA. And below these results I would like to have the results from tableB. There are no relations between these 2 tables. The columns are also differant. Here is an example:

tableA:                       tableB:

+-------------------------+   +--------------------------------+
| Name                        | Year
---------------------------   +---------------------------------
| A    |                      | 2000    |
| B    |                      | 2001    |
| C    |                      | 2002    |

I would like to have results like this:

 +-------------------------+   
    | Name | Year                   
    ------------------------
    | A    |                   
    | C    |
    | D    |
    |      | 2000
    |      | 2001
    |      | 2002

How can I realize this? I see only joins, union solutions, but they dont work for me because I dont have some relation between these 2 tables.

Best Answer

with cte1(Name, Year) as ( select Name, null as year from tablea),
cte2(Name, Year) as (select NULL as Name, year from tableb)
Select Name, Year from CTE1
UNION ALL
select Name, Year from CTE2

This should work, didn't read the question properly last answer :)