Reputation: 41
I would need to insert the non existing values to a Table.
BarcodeSubgroup
(single column table) nvarchar
)Sample data in the table
ProductSubGroupID
-----------------
F11WD
F77AH
G36CN
G37HJ
H11AA
H11AD
Now I need to insert the non existing values in the table.
Values want to be checked and inserted.
H11AA
H11AD
G78DE
G76DK
G41JA
B45JC
Query written
insert into BarcodeSubgroup
select
productsubgroupid
where
not exists ('G78DE', 'G76DK', 'G41JA',
'B45JC', 'H11AA', 'H11AD')
Now it should insert only the 4 non existing values.
Upvotes: 0
Views: 66
Reputation: 1269463
You can do this with select . . . not exists
:
insert into BarcodeSubgroup(productsubgroupid)
select productsubgroupid
from (values ('G78DE'), ('G76DK'), ('G41JA'), ('B45JC'), ('H11AA'), ('H11AD') ) v(productsubgroupid)
where not exists (select 1
from BarcodeSubgroup bs
where bs.productsubgroupid = v.productsubgroupid
);
Upvotes: 2