I have been looking for a solution to best implement a do not insert if the line already exists. I've read a lot of answers which al differ in some way and it's a little above my level to make any sense.
For example the following post is the top result: SQL Server Insert if not exist But I cannot understand why the variables are used and how I define table1 and table2. Would somebody be able to explain this a litle further so that I can apply it to my situation?
In my example I have about 5 columns (Date, Number1, Number2, Text1, Text2) that I'd like to insert from table1 to table2, but only if the do not exist. Update is not necessary. (Therefore merge is out of the question)
A not exists should work using a correlated subquery.
The sub select identifies all records in Table1 which do not exist in Table2 by joining on all the fields we would be inserting. This ensures we only insert records which do not already exist in table2.
Since we don't care what value is selected as it's throwaway from the subquery, I simply select 1 from the not exists correlated subquery.
Now if there are other columns we need to consider, then we would have to add those to our query, or if there's a primary key would could consider instead of all the columns, then we would only need to join on it.