Allen_Delon
Allen_Delon

Reputation: 37

Create a new table from two existing table

Hello,

I need your help. I am trying to create a new table using two existing table (TABLE1 and TABLE2). I have no idea what SQL Server technique I can use to resolve this issue.

SELECT A AS A1, B AS B1, C AS C1 FROM TABLE1
Result:
        A1  B1  C1
        3   4   1
        1   2   0

SELECT E2 AS A1, F2 AS B1, G2 AS C1 FROM TABLE2
Result:
        A1  B1  C1
        4   7   1
        2   8   6

Let’s name the new table 'My_Table' which includes all data from TABLE1 and TABLE2.

SELECT * FROM My_Table
Result:
        A1  B1  C1
        3   4   1
        1   2   0
        4   7   1
        2   8   6

I want to save this table in my database and use it in the future.

Any help would be appreciated.

Upvotes: 1

Views: 100

Answers (1)

robdjc
robdjc

Reputation: 128

You can try to use the INTO clause and a UNION of both selects.

SELECT A AS A1, B AS B1, C AS C1 
INTO My_Table
FROM TABLE1
UNION
SELECT E2 AS A1, F2 AS B1, G2 AS C1 
FROM TABLE2

Note, that a UNION will also remove duplicate records. If that is not expected, then check out UNION ALL.

Upvotes: 2

Related Questions