How to insert/combine values from multiple tables? I have multiple tables (12 for each month) that have 3 columns each ("code", "test_name", "value") 

Example:



January
code | test_name | value
0001   name1       17
0002   name2       45
0005   name5       12




February
code | test_name | value
0001   name1       3
0002   name2       7
0004   name4       13




March
code | test_name | value
0001   name1       6
0006   name6       32
0007   name7       41




How do I merge or insert or combine it in sql into one table that would look like this:

Year
code | test_name | January | February | March | ...
0001   name1       17        3          6
0002   name2       45        7          0
0003   name3       0         0          0
0004   name4       0         13         0
0005   name5       12        0          0
0006   name6       0         0          32
0007   name7       0         0          41
I wrote this for SQL 2012, but it should work in 2008R2 and up.

DECLARE @January AS TABLE
    (
      Code CHAR(4) NOT NULL
    , test_name VARCHAR(100) NOT NULL
    , value INT NOT NULL
    );

INSERT  INTO @January
        ( Code, test_name, value )
VALUES  ( '0001', 'name1', 17 )
    ,   ( '0002', 'name2', 45 )
    ,   ( '0005', 'name5', 12 );

DECLARE @February AS TABLE
    (
      Code CHAR(4) NOT NULL
    , test_name VARCHAR(100) NOT NULL
    , value INT NOT NULL
    );

INSERT  INTO @February
        ( Code, test_name, value )
VALUES  ( '0001', 'name1', 3 )
    ,   ( '0002', 'name2', 7 )
    ,   ( '0004', 'name4', 13 );

DECLARE @march AS TABLE
    (
      Code CHAR(4) NOT NULL
    , test_name VARCHAR(100) NOT NULL
    , value INT NOT NULL
    );

INSERT  INTO @march
        ( Code, test_name, value )
VALUES  ( '0001', 'name1', 6 )
    ,   ( '0006', 'name6', 32 )
    ,   ( '0007', 'name7', 41 );


WITH CTE_AllMonths AS
    (
    SELECT Code, test_name, value, 'January' AS SourceMonth
    FROM @January
    UNION ALL
    SELECT Code, test_name, value, 'February' AS SourceMonth
    FROM @February 
    UNION ALL
    SELECT Code, test_name, value, 'March' AS SourceMonth
    FROM @March  
    )
    SELECT Code, test_name, January, February, March
    FROM CTE_AllMonths 
        PIVOT (SUM(value) FOR SourceMonth IN ([January], [February], [March])) AS PT