134

I have the following two tables:

Table1
----------
ID   Name
1    A
2    B
3    C

Table2
----------
ID   Name
1    Z

I need to insert data from Table1 to Table2. I can use the following syntax:

INSERT INTO Table2(Id, Name) SELECT Id, Name FROM Table1

However, in my case, duplicate IDs might exist in Table2 (in my case, it's just "1") and I don't want to copy that again as that would throw an error.

I can write something like this:

IF NOT EXISTS(SELECT 1 FROM Table2 WHERE Id=1)
INSERT INTO Table2 (Id, name) SELECT Id, name FROM Table1 
ELSE
INSERT INTO Table2 (Id, name) SELECT Id, name FROM Table1 WHERE Table1.Id<>1

Is there a better way to do this without using IF - ELSE? I want to avoid two INSERT INTO-SELECT statements based on some condition.

Pang
  • 9,564
  • 146
  • 81
  • 122
Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134

11 Answers11

262

Using NOT EXISTS:

INSERT INTO TABLE_2
  (id, name)
SELECT t1.id,
       t1.name
  FROM TABLE_1 t1
 WHERE NOT EXISTS(SELECT id
                    FROM TABLE_2 t2
                   WHERE t2.id = t1.id)

Using NOT IN:

INSERT INTO TABLE_2
  (id, name)
SELECT t1.id,
       t1.name
  FROM TABLE_1 t1
 WHERE t1.id NOT IN (SELECT id
                       FROM TABLE_2)

Using LEFT JOIN/IS NULL:

INSERT INTO TABLE_2
  (id, name)
   SELECT t1.id,
          t1.name
     FROM TABLE_1 t1
LEFT JOIN TABLE_2 t2 ON t2.id = t1.id
    WHERE t2.id IS NULL

Of the three options, the LEFT JOIN/IS NULL is less efficient. See this link for more details.

nloewen
  • 1,279
  • 11
  • 18
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
  • 10
    Just a clarification on the NOT EXISTS version, you'll need a WITH(HOLDLOCK) hint or no locks will be taken (because there are no rows to lock!) so another thread could insert the row under you. – IDisposable Mar 25 '10 at 06:27
  • 3
    Interesting, because I have always believed joining to be faster than sub-selects. Perhaps that is for straight joins only, and not applicable to left joins. – Duncan Mar 25 '10 at 06:55
  • 1
    Duncan, joining is often faster that subselects when they are correlated subqueries. If you have the subquery up in the select list a join will often be faster. – HLGEM Mar 25 '10 at 13:25
  • Thanks! option 2 seems like it would be really inefficient. Unless the database is smart enough to know not to fetch the entire results of the subquery? – Kip Dec 13 '10 at 18:39
  • @Kip: If you read the link I provided that compares the three options, you'd know that your perception is not correct on SQL Server. Could be different on other databases, but the columns compared being nullable or not makes a difference too. – OMG Ponies Dec 13 '10 at 19:07
  • 11
    `NOT EXISTS` is especially useful with composite primary key, `NOT IN` won't work then – tomash Sep 30 '11 at 11:38
  • 1
    Any ideas why I would I still get `cannot insert duplicate key...` using any of the above methods? – Drew Chapin Mar 25 '14 at 07:49
  • @druciferre Possibly a duplicate within the source being inserted. – bvj Jul 31 '14 at 04:46
  • `NOT IN` also works in `MYSQL Server` – bishop Jan 16 '23 at 10:40
50

In MySQL you can do this:

INSERT IGNORE INTO Table2(Id, Name) SELECT Id, Name FROM Table1

Does SQL Server have anything similar?

Duncan
  • 2,056
  • 13
  • 11
7

I just had a similar problem, the DISTINCT keyword works magic:

INSERT INTO Table2(Id, Name) SELECT DISTINCT Id, Name FROM Table1
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
Hunter Bingham
  • 119
  • 1
  • 1
  • 31
    Unless I totally misunderstand you, this will work if you have duplicates in the set you're inserting _from_. It won't, however, help if the set you're inserting from might be duplicates of data already in the `insert into` table. – FreeMan Jan 24 '17 at 19:20
5

I was facing the same problem recently...
Heres what worked for me in MS SQL server 2017...
The primary key should be set on ID in table 2...
The columns and column properties should be the same of course between both tables. This will work the first time you run the below script. The duplicate ID in table 1, will not insert...

If you run it the second time, you will get a

Violation of PRIMARY KEY constraint error

This is the code:

Insert into Table_2
Select distinct *
from Table_1
where table_1.ID >1
zx485
  • 28,498
  • 28
  • 50
  • 59
Zoloholic
  • 51
  • 1
  • 3
4

Using ignore Duplicates on the unique index as suggested by IanC here was my solution for a similar issue, creating the index with the Option WITH IGNORE_DUP_KEY

In backward compatible syntax
, WITH IGNORE_DUP_KEY is equivalent to WITH IGNORE_DUP_KEY = ON.

Ref.: index_option

Community
  • 1
  • 1
Tazz602
  • 51
  • 3
4

From SQL Server you can set a Unique key index on the table for (Columns that needs to be unique)

From sql server right click on the table design select Indexes/Keys

Select column(s) that will be not duplicate , then type Unique Key

M. Salah
  • 671
  • 7
  • 10
2

A little off topic, but if you want to migrate the data to a new table, and the possible duplicates are in the original table, and the column possibly duplicated is not an id, a GROUP BY will do:

INSERT INTO TABLE_2
(name)
  SELECT t1.name
  FROM TABLE_1 t1
  GROUP BY t1.name
FullStackFool
  • 1,071
  • 9
  • 15
0

In my case, I had duplicate IDs in the source table, so none of the proposals worked. I don't care about performance, it's just done once. To solve this I took the records one by one with a cursor to ignore the duplicates.

So here's the code example:

DECLARE @c1 AS VARCHAR(12);
DECLARE @c2 AS VARCHAR(250);
DECLARE @c3 AS VARCHAR(250);


DECLARE MY_cursor CURSOR STATIC FOR
Select
c1,
c2,
c3
from T2
where ....;

OPEN MY_cursor
FETCH NEXT FROM MY_cursor INTO @c1, @c2, @c3

WHILE @@FETCH_STATUS = 0
BEGIN
    if (select count(1) 
        from T1
        where a1 = @c1
        and a2 = @c2
        ) = 0 
            INSERT INTO T1
            values (@c1, @c2, @c3)

    FETCH NEXT FROM MY_cursor INTO @c1, @c2, @c3
END
CLOSE MY_cursor
DEALLOCATE MY_cursor
0

I used a MERGE query to fill a table without duplications. The problem I had was a double key in the tables ( Code , Value ) , and the exists query was very slow The MERGE executed very fast ( more then X100 )

examples for MERGE query

Shoham
  • 40
  • 1
  • 2
  • 7
0

For one table it works perfectly when creating one unique index from multiple field. Then simple "INSERT IGNORE" will ignore duplicates if ALL of 7 fields (in this case) will have SAME values.

Select fields in PMA Structure View and click Unique, new combined index will be created.

enter image description here

Gediminas Šukys
  • 7,101
  • 7
  • 46
  • 59
-6

A simple DELETE before the INSERT would suffice:

DELETE FROM Table2 WHERE Id = (SELECT Id FROM Table1)
INSERT INTO Table2 (Id, name) SELECT Id, name FROM Table1

Switching Table1 for Table2 depending on which table's Id and name pairing you want to preserve.

Sacro
  • 11
  • 3
  • 6
    Please don't do this. You're basically saying "whatever data I had is worthless, let's just insert this new data!" – Andir Dec 20 '18 at 19:19
  • @Andir If for some reason "Table2" shouldn't getting dropped after the "INSERT" then use the other methods, but this is a perfectly valid way to achieve what the OP asked. – Sacro Jan 02 '19 at 07:58
  • 1
    Valid, but certainly slower and potentially corrupting without a transaction. If you go this route, wrap in a TRANSaction. – MC9000 Jun 10 '20 at 20:17