複数の ODBC テーブルへのリンクを持つ Access 2010 データベースがあります。テーブルを最初にリンクしたときにユーザー名とパスワードがキャッシュされたので、クエリを実行するたびにログイン 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*"));
これが同じ問題に遭遇した人の助けになることを願っていますが、なぜこれが起こっているのか、なぜこの修正が機能するのかについて誰かが光を当てることができれば、私はそれを大いに感謝します.