0

我有一个 Access 2010 数据库,其中包含多个 ODBC 表的链接。当我第一次链接表时,我的用户名和密码被缓存了,所以我每次运行查询时都不必重新输入我的登录 ID 和密码——除了一个。我有 20 多个查询,无需重新登录即可正常工作。

我的 SQL 在下面。代码中是否存在本质上的问题,还是另一个问题?

SELECT DISTINCT IIf(IsNull([LeftSide]![ContactID]),"",CStr([LeftSide]![ContactID])) AS LeftSideContactID, IIf(IsNull([rightSide]![ContactID]),"",CStr([rightSide]![ContactID])) AS RightSideContactID, [RightSide]![Pref] & " & " & [LeftSide]![Pref] AS [Combined Prefix], [RightSide]![FName] & " & " & [LeftSide]![FName] AS [Combined FName], IIf([RightSide]![LName]=[LeftSide]![LName],[RightSide]![LName],[RightSide]![LName] & " & " & [LeftSide]![LName]) AS [Combined LName], [RightSide]![Pref] & " " & [RightSide]![FName] & " " & [RightSide]![LName] & " & " & [LeftSide]![Pref] & " " & [LeftSide]![FName] & " " & [LeftSide]![LName] AS [Combined Mailing Name]
FROM (([Contacts In Query (username)] AS LeftSide INNER JOIN ContactContact ON LeftSide.ContactID = ContactContact.Contact1ID) INNER JOIN [Contacts In Query (username)] AS RightSide ON ContactContact.Contact2ID = RightSide.ContactID) INNER JOIN Relationship ON ContactContact.RelationshipID = Relationship.RelationshipID
WHERE (((Relationship.Relationship) Like "*spouse*"));

[Contacts In Query (username)] 是 Access 中的平面表,其他(关系和 ContactContact)是从外部数据库链接的。

我有一个解决方法 - 奇怪的是,只要我将连接从 更改INNER JOINLEFT JOINand RIGHT JOIN,它就可以在不需要登录的情况下工作。然后,我不得不使用几个标准通过过滤掉不完全匹配的结果来近似内部连接。

SELECT DISTINCT IIf(IsNull([LeftSide]![ContactID]),"",CStr([LeftSide]![ContactID])) AS LeftSideContactID, IIf(IsNull([rightSide]![ContactID]),"",CStr([rightSide]![ContactID])) AS RightSideContactID, [RightSide]![Pref] & " & " & [LeftSide]![Pref] AS [Combined Prefix], [RightSide]![FName] & " & " & [LeftSide]![FName] AS [Combined FName], IIf([RightSide]![LName]=[LeftSide]![LName],[RightSide]![LName],[RightSide]![LName] & " & " & [LeftSide]![LName]) AS [Combined LName], [RightSide]![Pref] & " " & [RightSide]![FName] & " " & [RightSide]![LName] & " & " & [LeftSide]![Pref] & " " & [LeftSide]![FName] & " " & [LeftSide]![LName] AS [Combined Mailing Name]
FROM (([Contacts In Query (username)] AS LeftSide RIGHT JOIN ContactContact ON LeftSide.ContactID = ContactContact.Contact1ID) LEFT JOIN [Contacts In Query (username)] AS RightSide ON ContactContact.Contact2ID = RightSide.ContactID) INNER JOIN Relationship ON ContactContact.RelationshipID = Relationship.RelationshipID
WHERE (((IIf(IsNull([LeftSide]![ContactID]),"",CStr([LeftSide]![ContactID])))<>"") AND ((IIf(IsNull([rightSide]![ContactID]),"",CStr([rightSide]![ContactID])))<>"") AND ((Relationship.Relationship) Like "*spouse*"));

希望这会帮助遇到同样问题的任何人,但如果有人能解释为什么会发生这种情况,以及为什么这个修复工作有效,我将不胜感激。

4

1 回答 1

1

听起来其中一个隐藏的系统表中可能存在一些数据库损坏。您是否尝试过将对象导入新的 Access 数据库,然后运行原始查询?损坏的对象不会成功导入。

于 2014-08-26T17:05:53.073 回答