我有一个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中的一个平面表,其他的(Relationship和ContactContact)是从外部数据库链接的。
我有一个解决方案-奇怪的是,只要我改变连接从INNER JOIN
到LEFT JOIN
和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*"));
希望这能帮助任何遇到同样问题的人,但是如果有人能解释为什么会发生这种情况,以及为什么这个修复工作,我将非常感激。
听起来可能在一个隐藏的系统表中有一些数据库损坏。您是否尝试过将对象导入到新的Access数据库中,然后运行原始查询?损坏的对象无法导入。