sql server - SQL Insert Into Where Record Not Exists -
i have automated process appends data table1
every week. has 4 columns [reason, number of occurences, sent date, , program]
. table1
keeps growing , not care duplicate records. (there 26 records each week , 4 columns performance/space not issue)
i have table table2
want distinct records table1
. if record exists in table 2
not want insert record. thought below statement work, not:
begin transaction insert [database]..[table2] select distinct * [database]..[table1] not exists (select * [database]..[table2]) (0 row(s) affected)
if comment out clause, work, insert records exist in table2
begin transaction insert [database]..[table2] select distinct * [database]..[table1] --where not exists (select * [database]..[table2]) (83 row(s) affected)
how check distinct records in table1
, if record not exist in table2
, insert record?
i using ms sql server version 11.0.6020.0
in sql server, use except
. assuming tables have same columns:
insert [database]..[table2] select distinct t1.* [database]..[table1] t1 except select t2.* [database]..[table2] t2;
your not exists
clause not correlated data in table1
, not doing expect.
Comments
Post a Comment