导入访问csv没有重复

时间:2014-02-20 23:34:40

标签: sql ms-access csv ms-access-2007 ms-access-2013

我有一个以.csv格式提供给我的股票清单。我无法更改csv,需要通过Access 2007-2013进行所有验证。

我已经创建了一个追加查询,可以将我的数据从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];

我需要它做的是导入没有重复,似乎解决方案是将数据导入临时表,然后从那里复制没有欺骗。

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取自:Copy rows from one table to another, ignoring duplicates

然而,这似乎是一个糟糕的解决方案。有没有办法在查询中使用左连接来同时完成这两个任务?

编辑:我取得了一些进展。

  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));

编辑:解决方案

  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));

1 个答案:

答案 0 :(得分:0)

“我需要的是一个AND语句,只有在所有字段相等的情况下才排除行”

你似乎已经有了正确的想法?

我在WHERE子句中使用OR作为单个不匹配意味着它是一个不同的记录

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
);