SQL访问的基本问题

时间:2011-01-18 11:36:23

标签: ms-access

你好专家,

我有两个mdb数据库,比如old.mdb和new.mdb,它们具有相同的表和结构。我只想验证两个数据库中一个特定表中哪些记录被更改。我google了很长一段时间,最后遇到了以下SQL脚本:

SELECT Min(tmp.TableName) AS TableName, tmp.ID, tmp.CO1
FROM ( SELECT RECIPETABLE1 IN 'C:\MyRecipes\old.mdb' as TableName, sc_head.ID, sc_head.CO1
FROM sc_head
UNION ALL
SELECT RECIPETABLE1 in 'C:\MyRecipes\new.mdb ' as TableName, compare_sc_head.ID, compare_sc_head.CO1
FROM compare_sc_head
) AS tmp
GROUP BY tmp.ID, tmp.CO1
HAVING Count(*)=1
ORDER BY tmp.ID

Bei跑步,我收到一条错误,上面写着“In-Operator without()”

任何见解都很值得。

约翰

1 个答案:

答案 0 :(得分:1)

尝试:

SELECT Min(tmp.TableName) AS TableName, tmp.ID, tmp.CO1
FROM 
    (SELECT 'RECIPETABLE1'  as TableName, sc_head.ID, sc_head.CO1
     FROM sc_head IN 'C:\MyRecipes\old.mdb'
     UNION ALL
     SELECT 'RECIPETABLE2'  as TableName, compare_sc_head.ID, compare_sc_head.CO1
     FROM compare_sc_head in 'C:\MyRecipes\new.mdb') As tmp
GROUP BY tmp.ID, tmp.CO1
HAVING Count(*)=1
ORDER BY tmp.ID

但是,我建议使用一点VBA进行比较会更容易。

使用查询设计窗口将单个表与少量字段进行比较并不太令人不愉快,但是,对于更多字段表,使用VBA要容易得多。

以下是一些将在查询设计窗口中运行的查询。

在NewDB中记录但在OldDB中记录

SELECT n.ID, n.CO1
FROM 
   (SELECT ID, CO1
    FROM compare_sc_head 
    IN 'C:\MyRecipes\new.mdb') AS n 
LEFT JOIN 
   (SELECT ID, CO1
    FROM sc_head 
    IN 'C:\MyRecipes\old.mdb') AS o 
ON n.ID = o.ID
WHERE o.ID Is Null

在OldDB中记录,但在NewDB中记录

SELECT o.ID, o.CO1
FROM 
   (SELECT ID, CO1
    FROM sc_head 
    IN 'C:\MyRecipes\old.mdb') AS o 
LEFT JOIN 
   (SELECT ID, CO1
    FROM compare_sc_head 
    IN 'C:\MyRecipes\new.mdb') AS n 
ON o.ID = n.ID
WHERE n.ID Is Null

记录ID匹配但CO1不匹配

SELECT n.ID, n.CO1, o.CO1
FROM 
   (SELECT ID, CO1
    FROM sc_head 
    IN 'C:\MyRecipes\old.mdb') AS o 
INNER JOIN 
   (SELECT ID, CO1
    FROM compare_sc_head 
    IN 'C:\MyRecipes\new.mdb') AS n 
ON o.ID = n.ID
WHERE n.CO1 & "" <> o.CO1 & ""