2014-03-27 59 views
0

尋找正確的語法,如何獲取該做的所有記錄「NOT EXISTS」在主數據庫,但在遠程DB獲取記錄NOT IN本地數據庫,但在遠程數據庫

做到以下代碼獲取所有空白記錄從遠程數據庫不確定它是否與本地數據庫相關。

<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="<%$ ConnectionStrings:accessConnectionString %>"  
    SelectCommand="SELECT EnrollmentsTbl.UserName, EnrollmentsTbl.SubmitTime, EnrollmentsTbl.ClassName, 
    EnrollmentsTbl.ClassDate, EnrollmentsTbl.ClassTime, EnrollmentsTbl.Enrolled, EnrollmentsTbl.WaitListed, EnrollmentsTbl.Instructor, 
    EnrollmentsTbl.DateCompleted, EnrollmentsTbl.Completed, EnrollmentsTbl.Walkin 
    FROM [;DATABASE=e:\subsite.mdb].EnrollmentsTbl AS XXX 
    LEFT JOIN EnrollmentsTbl ON XXX.UserName = EnrollmentsTbl.UserName WHERE (EnrollmentsTbl.UserName Is Null)"> 
</asp:AccessDataSource> 

enter image description here

+0

[在運算符中沒有()在查詢表達式中]的可能重複(http://stackoverflow.com/questions/22698305/in-operator-wi在查詢中使用表達式) – Brad

+0

示例代碼不會失敗?它只是返回不正確的記錄 – BarclayVision

回答

2

「標準」 的方法是使用外部聯接和過濾器對空,意譯爲:

select r.* 
from remote_table r 
left join local_table l 
    on l.id = r.id 
where l.id is null 
+0

我仍然需要WHERE子句,因爲它是一個LEFT JOIN? – BarclayVision

+0

Where子句對此工作至關重要。當連接未命中(意味着沒有本地行)時,左側連接的行的所有列都將爲空,並且您使用該事實過濾掉命中,只留下未命中,這正是您想要的。 – Bohemian

+0

只會出現錯誤:「沒有給出一個或多個必需參數的值。」 – BarclayVision

0

這裏是工作的最終解決方案:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="<%$ ConnectionStrings:accessConnectionString %>"  
    SelectCommand="SELECT XXX.UserName, XXX.SubmitTime, XXX.ClassName, 
    XXX.ClassDate, XXX.ClassTime, XXX.Enrolled, XXX.WaitListed, XXX.Instructor, 
    XXX.DateCompleted, XXX.Completed, XXX.Walkin 
    FROM [;DATABASE=e:\subsite.mdb].EnrollmentsTbl AS XXX 
    LEFT JOIN Users ON XXX.UserName = Users.UserName WHERE (Users.UserName Is Null)"> 
</asp:AccessDataSource>