17

I have a sql table that has two columns id and name. I have list of names about 20 and I need to write a query that checks if name exists before insert.

Is there a better way of doing this rather then just having the below query 20 times but with different names (I need do this in t-sql):

IF NOT EXISTS(SELECT* 
              FROM   mytable 
              WHERE  name = 'Dan') 
  BEGIN 
      INSERT INTO mytable 
                  (name) 
      VALUES     ('dan') 
  END 
Drew
  • 2,583
  • 5
  • 36
  • 54
user3884462
  • 191
  • 1
  • 1
  • 7

7 Answers7

26
INSERT INTO MyTable (Name)
SELECT  NewNames.Name
FROM    ( VALUES ('Name1'), ('Name2'), ('Name3') ) AS NewNames (Name)
WHERE   NOT EXISTS ( SELECT 1
                     FROM   MyTable AS MT
                     WHERE  MT.Name = NewNames.Name );
KSM
  • 111
  • 1
  • 8
Ralf Hundewadt
  • 1,078
  • 1
  • 13
  • 25
  • Is there a limit on how many values you can have in the ``from`` clause? Can it be in the millions? – Meitham Mar 10 '22 at 15:21
9

I think you could use a merge statement:

MERGE INTO myTable AS Target
USING (VALUES ('name1'),('name2'),('...')) AS source (NAME)
ON Target.NAME = Source.NAME
WHEN NOT MATCHED BY TARGET THEN
INSERT (NAME) VALUES (name)
Dave.Gugg
  • 6,561
  • 3
  • 24
  • 43
5

You can filter values with NOT EXISTS

INSERT INTO myTable (
    Name
)
SELECT DISTINCT
    Name
FROM (
        VALUES ('Name 1'),
               ('Name 2')
    ) AS NewNames(Name)
WHERE
    NOT EXISTS (SELECT 1 FROM TargetTable WHERE myTable.Name = NewNames.Name)

If your new names are in another table, you can change the select query in the above one.

Please note, that the DISTINCT keyword is necessary to filter out the duplications in the source data.

Pred
  • 8,789
  • 3
  • 26
  • 46
  • It worked for me! If anyone is trying to do same for tables with multiple columns, you can do as: INSERT INTO myTable ( name, code ) SELECT DISTINCT name, code FROM ( VALUES ('Name 1','Code 1'), ('Name 2','Code2') ) AS NewNames(name, code) WHERE NOT EXISTS (SELECT 1 FROM myTable m WHERE m.name = NewNames.name) – Sabu Shakya Jan 12 '22 at 04:49
3

I would do this using insert:

with names as (
      select 'Dan' as name union all
      select 'name2' union all
      . . .
     )
insert into myTable(name)
    select distinct name
    from myTable
    where not exists (select 1 from mytable t2 where t2.name = t.name);

Note: you may want to create a unique index on mytable(name) so the database does the checking for duplicates.

JDawg
  • 8,560
  • 4
  • 25
  • 29
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
2
INSERT INTO MyTable (Name)
SELECT Name FROM
(
    VALUES ('Name 1'),
           ('Name 2')
) AS Names(Name)
WHERE Name NOT IN
(
    SELECT Name FROM MyTable
)
Radu Porumb
  • 785
  • 5
  • 7
1

untested so there might be some minor errors:

merge into mytable x
using (
    values ('name1')
         , ('name2')
         , ...
         , ('namen')
) as y (name)
    on x.name = y.name
when not matched then 
    insert (name)
    values (y.name)
Lennart - Slava Ukraini
  • 6,936
  • 1
  • 20
  • 32
1

INSERT IGNORE INTO myTable (column1, column2) VALUES (val1, val2),(val3,val4),(val5,val6);

INSERT IGNORE will allow skip on duplicate values

Jacek Pietal
  • 1,980
  • 1
  • 18
  • 27
  • 2
    IGNORE doesn't exist in SQL SERVER. Similar functionality being: http://stackoverflow.com/a/21220868/1918179 – Izzy Nov 18 '15 at 11:06