SQL Server insert into non duplicates -
table objects:
create table objects( id int identity primary key, name varchar(50) unique )
lets have in table records a,b,c
. inserting new rows given string example 'd,e,f,c,b,d'
, using function changing csv string table , returning it.
i need insert string example not in objects table, d,e,f
. how can it? thinking about
insert objects(names) select distinct names split('d,e,f,c,b,d') except select names objects
but sure has better sollution.
i used merge
, works
merge objects using (select distinct name split(@string,',')) s on objects.name=s.name when not matched insert(name) values (split.name);
Comments
Post a Comment