2012-07-30 91 views
-1

我在不同的地方發現了這個錯誤,我不知道爲什麼所以我想知道這裏的任何人是否可能有任何線索。這是sql。無法綁定多部分標識

select Intranet.dbo.tblPeople.FirstName, 
    Intranet.dbo.tblPeople.LastName, 
    Intranet.dbo.tblPeople.CellPhone 
    tblActivities.OnSiteSupervisorID, 
    tblActivities.OffSiteSupervisorID, 
    tblProjects.projectid, 
    tblVehicleData.Description AS vehicle, 
    tblVehicleData.VehicleID, 
    tblVehicleData.ActivityID as ActivityID, 
    tblProjects.ProjectName, 
    tblActivities.numPersons, 
    tblActivities.Description, 
    tblActivities.ChannelNumber, 
    tblActivities.StartDate, 
    tblActivities.EndDate, 
    tblActivities.estimatedExitTime, 
    tblLocations.locationname, 
from Intranet.dbo.tblPeople, 
    smartroadmangement.dbo.tblVehicleData, 
    smartroadmangement.dbo.tblProjects, 
    smartroadmangement.dbo.tblActivities 
left join smartroadmangement.dbo.tblLocations 
    on smartroadmangement.dbo.tblActivities.locationId = smartroadmangement.dbo.tblLocations.locationid 
left join Intranet.dbo.tblPeople as people 
    on people.PersonID = smartroadmangement.dbo.tblActivities.OnSiteSupervisorID 
left join tblActivities as Activities 
    on Activities.ActivityID = tblVehicleData.ActivityID 
left join tblActivities as Activitiez 
    on Activitiez.ProjectID = tblProjects.ProjectID 
WHERE (smartroadmangement.dbo.tblActivities.StartDate Between '2012-07-15 14:17:09' and '2012-07-15 20:17:09') 
    AND (smartroadmangement.dbo.tblActivities.EndDate Between '2012-07-15 20:17:09'and '2012-07-15 23:17:09') 
+0

哪個標識符有問題? – 2012-07-30 20:44:52

+0

忘了補充我收到這兩行的錯誤。 左加入tblActivities作爲活動上Activities.ActivityID = tblVehicleData.ActivityID 左加入tblActivities作爲Activitiez上Activitiez.ProjectID = tblProjects.ProjectID – Guyver 2012-07-30 20:45:14

+0

我很困惑,爲什麼你在FROM有多個表,然後加入他們明確了。我建議你閱讀這個問題關於你使用隱式連接http://stackoverflow.com/questions/894490/sql-left-join-vs-multiple-tables-on-from-line – hatchet 2012-07-30 20:49:44

回答

5

我的第一個建議是在所有的JOIN上使用正確的JOIN語法。您正在使用逗號和JOIN語法進行連接,因此很難調試您的查詢。

您的評論說你在這裏得到的錯誤:

left join tblActivities as Activities 
    on Activities.ActivityID = tblVehicleData.ActivityID 
left join tblActivities as Activitiez 
    on Activitiez.ProjectID = tblProjects.ProjectID 

那是表實際上在查詢smartroadmangement.dbo.tblActivities

根據您的評論如下,你可能會想嘗試:

left join smartroadmangement.dbo.tblActivities as Activities 
    on Activities.ActivityID = tblVehicleData.ActivityID 
left join smartroadmangement.dbo.tblActivities as Activitiez 
    on Activitiez.ProjectID = tblProjects.ProjectID 

這裏是正確的JOIN語法更新查詢:

select ppl.FirstName, 
    ppl.LastName, 
    ppl.CellPhone 
    a1.OnSiteSupervisorID, 
    a1.OffSiteSupervisorID, 
    tblProjects.projectid, 
    v.Description AS vehicle, 
    v.VehicleID, 
    v.ActivityID as ActivityID, 
    tblProjects.ProjectName, 
    a1.numPersons, 
    a1.Description, 
    a1.ChannelNumber, 
    a1.StartDate, 
    a1.EndDate, 
    a1.estimatedExitTime, 
    l.locationname, 
from Intranet.dbo.tblPeople ppl 
left join smartroadmangement.dbo.tblActivities a1 
    on ppl.PersonID = a1.OnSiteSupervisorID 
left join smartroadmangement.dbo.tblLocations l 
    on a1.locationId = l.locationid 
left join smartroadmangement.dbo.tblVehicleData v 
    on a1.ActivityID = v.ActivityID 
left join smartroadmangement.dbo.tblProjects pj 
    on a1.ProjectID = pj.ProjectID 
WHERE (a1.StartDate Between '2012-07-15 14:17:09' and '2012-07-15 20:17:09') 
    AND (a1.EndDate Between '2012-07-15 20:17:09'and '2012-07-15 23:17:09') 
+0

在我的?是 – Guyver 2012-07-30 21:32:15

+0

@Guyver看到我的編輯 – Taryn 2012-07-30 21:36:28

+0

沒有運氣:/仍然在相同的點出現相同的錯誤。 – Guyver 2012-07-30 21:40:47

0

您正在使用四部分名稱。您是否設置了名爲smartroadmangement的鏈接服務器/別名?

+0

我正在使用類似smartroadmangement.dbo的東西,但它似乎並不介意我不使用它在數據庫中的東西。只是從我使用的另一個人的東西。 – Guyver 2012-07-30 20:51:02

0

雖然我覺得您的查詢不應該有隱式連接(FROM之後的多個表)和顯式連接(請參閱bluefeet關於它應該如何查看的答案),以下語法錯誤可能是查詢綁定錯誤的原因:

你有...

from Intranet.dbo.tblPeople, 
    smartroadmangement.dbo.tblVehicleData, 
    smartroadmangement.dbo.tblProjects, 
    martroadmangement.dbo.tblActivities 

但你可能打算 「smartroadmangement.dbo.tblActivities」,而不是 「martroadmangement.dbo.tblActivities」。

相關問題