9

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!

1

6 Answers 6

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 nice answer. Plz add explanation if possible that will be good :)
4

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; 

1 Comment

Thanks Simon1979! Here we do need UNION ALL otherwise some data are missing!
2

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

Comments

2
create table Amide_actives_decoys
as
select Structure, Name, Active from 
(
SELECT * FROM Amide_decoys 
UNION
SELECT * FROM Amide_actives 
)
;

1 Comment

It's best to explain your answers rather than just post code, as the OP may not entirely follow a code only answer.
1

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

1 Comment

Good and detailed answer too! However, Here we do need UNION ALL otherwise some data are missing!
0

SELECT tab1.firstName, tab1.lastName, tab2.city, tab2.state FROM TABLE_1 tab1 LEFT JOIN TABLE_2 tab2 ON tab1.personId=tab2.personId

1 Comment

Hey, welcome to SO! Thank you for your contribution! In your future answers, please consider adding some explanations along with the code snippet. Also, you can use single or triple backticks to escape code snippets and even have code highlighting!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.