2011-08-03 110 views
0

我有一個動態生成的查詢。查詢連接差異

SELECT '' + CAST(GalleryGallery_tGallery._Name AS VARCHAR(4000)) + '' AS NewName 
FROM Photographers_tGalleries 
     LEFT OUTER JOIN Gallery_tGallery AS GalleryGallery_tGallery 
     ON BaseContent_tGalleries.[Gallery] = GalleryGallery_tGallery._Guid 
     LEFT OUTER JOIN BaseContent_tGalleries 
     ON Photographers_tGalleries._Guid = 
      BaseContent_tGalleries._Guid_Structure_Content 

這些連接對我來說似乎是正確的。但是,查詢錯誤與The multi-part identifier "BaseContent_tGalleries.Gallery" could not be bound.

以下查詢不起作用。雖然連接匹配正確的字段,但它們的順序不同。我想知道爲什麼這個工作,其他不工作。我們希望修復最大的一個,但由於它是動態的,我正在尋找最少的變化。

SELECT '' + CAST(GalleryGallery_tGallery._Name AS VARCHAR(4000)) + '' AS NewName 
FROM Gallery_tGallery AS GalleryGallery_tGallery 
     LEFT OUTER JOIN BaseContent_tGalleries 
     ON GalleryGallery_tGallery._Guid = BaseContent_tGalleries.Gallery 
     LEFT OUTER JOIN Photographers_tGalleries 
     ON BaseContent_tGalleries._Guid_Structure_Content = 
      Photographers_tGalleries._Guid 

回答

3

您對第一個查詢的加入排序是錯誤的。您需要在Gallery_tGallery之前引用BaseContent_tGalleries。

SELECT '' + CAST(g._Name AS VARCHAR(4000)) + '' AS NewName 
FROM Photographers_tGalleries AS g 
     LEFT OUTER JOIN BaseContent_tGalleries AS b 
     ON g._Guid = b._Guid_Structure_Content 
     LEFT OUTER JOIN Gallery_tGallery AS gg 
     ON b.[Gallery] = gg._Guid; 

誰的方式命名錶和別名? GalleryGallery_tGallery,真的嗎?我已經轉換爲較短的別名來彌補真正喜歡打字的人。很多。

+1

我知道這是瘋狂的,這些都是dailywtf類型的東西。我決定重寫整個功能。謝謝你的幫助 –

3

第一個查詢不起作用,因爲您試圖在ON語句中使用表BaseContent_tGalleries,但它尚未加入。換句話說,您使用表作爲連接條件,但表本身尚未加入。