Question

I have a stock list that is given to me in .csv format. I cannot change the csv and need to do all my validation through Access 2007-2013.

I've created an append query to import my data into a table from CSV.

INSERT INTO Table1 ( tblfield1, tblfield2, tblfield3, tblfield4 )
SELECT [File#csv].[columnname1], [File#csv].[columnname2], [File#csv].[columnname3], [File#csv].[columnname4]
FROM [File#csv] IN '' [Text;FMT=Delimited;HDR=YES;CharacterSet=437
;DATABASE=c:\your\filepath\here];

What I need it to do is import without duplicates, it seems a solution is to import the data to a temp table then copy without dupes from there.

INSERT INTO destTable
SELECT Field1,Field2,Field3,... 
FROM srcTable
WHERE NOT EXISTS(SELECT * 
                 FROM destTable 
                 WHERE (srcTable.Field1=destTable.Field1 and
                       SrcTable.Field2=DestTable.Field2...etc.)
                 )

(SQL taken from: Copy rows from one table to another, ignoring duplicates)

This seems like a poor solution however. Is there a way to use a left join in the query to accomplish both tasks at once?

EDIT: I have made some progress.

  INSERT INTO tblDestination ( destfield1, destfield2, destfield3, destfield4 )
    SELECT filename1.[field1], filename1.[(field2], filename1.[field3], filename1.[field4]
    FROM [Text;FMT=Delimited;HDR=YES;CharacterSet=437)
    ;DATABASE=D:\your\file\path].filename.csv AS filename1 LEFT JOIN tblDestination ON filename1.[fieldX] = tblDestination.destfieldX
    WHERE (((filename1.[FieldX])= Is Null));

Edit: Solution is

  INSERT INTO tblDestination ( destfield1, destfield2, destfield3, destfield4 )
    SELECT filename1.[field1], filename1.[(field2], filename1.[field3], filename1.[field4]
    FROM [Text;FMT=Delimited;HDR=YES;CharacterSet=437)
    ;DATABASE=D:\your\file\path].filename.csv AS filename1 LEFT JOIN tblDestination ON filename1.[fieldX] = tblDestination.destfieldX
WHERE (((tblDestination.Field1) Is Null) AND ((tblDestination.Field2) Is Null));
Was it helpful?

Solution

"What I need though is an AND statement to only exclude the row if ALL fields are equal"

You seem to already have the right idea?

I'm using OR in the WHERE clause as a single mismatch means it's a different record

INSERT INTO tblDestination 
(destfield1, destfield2, destfield3, destfield4 )
SELECT filename1.[field1], filename1.[(field2], filename1.[field3], filename1.[field4]
FROM [Text;FMT=Delimited;HDR=YES;CharacterSet=437;
DATABASE=D:\your\file\path].filename.csv] AS filename1 
LEFT JOIN tblDestination 
ON  filename1.[field1] = tblDestination.destfield1
AND filename1.[field2] = tblDestination.destfield2
AND filename1.[field3] = tblDestination.destfield3
AND filename1.[field4] = tblDestination.destfield4
WHERE (
filename1.[Field1] IS NULL OR
filename1.[Field2] IS NULL OR 
filename1.[Field3] IS NULL OR 
filename1.[Field4] IS NULL
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top