'Merge 2 tables in SQL and save into 1 new table
If we have two or more tables with the same columns
Table 1
Structure, Name, Active
1,A,1
Table 2
Structure, Name, Active
2,B,0
We would like to combine these two tables and save it into a new one
New Table
Structure, Name, Active
1,A,1
2,B,0
Here is the code
CREATE TABLE Amide_actives_decoys
(
Structure NVARCHAR(255),
Name NVARCHAR(255),
Active INT
)
GO
INSERT Amide_actives_decoys
FROM (
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
)
The following error message will show up
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'FROM'.
The same thing if we use
SELECT * INTO Amide_actives_decoys
FROM (
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
)
Following this answer
Joining a table onto itself in SQL and saving the result
The error message will be
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ';'.
Could any guru kindly offer some comments? Thanks!
Solution 1:[1]
This syntax works in different databases:
INSERT INTO Amide_actives_decoys(Structure, Name, Active)
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives;
In this form of INSERT, the output of the subquery becomes the set of input values for the INSERT.
Note that the datatypes for the expressions in the SELECT statement subquery must match the datatypes in the target table of the INSERT statement.
All of the rows returned by the subquery are inserted into the Amide_actives_decoys table.
If any one row fails the INSERT due to a constraint violation or datatype conflict, the entire INSERT fails and no rows are inserted.
Any valid subquery may be used within the INSERT statement.
Solution 2:[2]
I think you need to UNION ALL otherwise you may not capture all the data; depends on what data is in the table (duplicates etc).
INSERT INTO Amide_actives_decoys(Structure, Name, Active)
SELECT * FROM Amide_decoys
UNION ALL
SELECT * FROM Amide_actives;
Solution 3:[3]
create table Amide_actives_decoys
as
select Structure, Name, Active from
(
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
)
;
Solution 4:[4]
The General syntax is
INSERT INTO table2
SELECT * FROM table1;
you can SELECT INTO Statement in this Case
with cte as (select 1 col1 ,2 col2
union all
select 2,3)
select * into #tabletest from cte
select *From #tabletest
Solution 5:[5]
In both your answers, the issue is that you have not given an alias name for the table as a result.I think you missed an 'INTO' in the INSERT statement as well.
Query 1:
CREATE TABLE Amide_actives_decoys
(
Structure NVARCHAR(255),
Name NVARCHAR(255),
Active INT
)
GO
INSERT INTO Amide_actives_decoys
SELECT *
FROM (
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
) LU --LU is added.
For Query 1, the below also is correct
INSERT INTO Amide_actives_decoys
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
Query 2:
SELECT *
INTO Amide_actives_decoys
FROM (
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
) LU -- LU added
Solution 6:[6]
SELECT tab1.firstName, tab1.lastName, tab2.city, tab2.state FROM TABLE_1 tab1 LEFT JOIN TABLE_2 tab2 ON tab1.personId=tab2.personId
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Multisync |
| Solution 2 | Simon1979 |
| Solution 3 | SuperBiasedMan |
| Solution 4 | King_Fisher |
| Solution 5 | Jithin Shaji |
| Solution 6 | Vihang Chandra |
