SQL:内部和左侧连接多部分标识符错误

时间:2011-10-30 13:50:48

标签: sql sql-server database join

我有两个这样的查询;

SELECT  j.ID, j.Pkey, j.SUMMARY, j.CREATED, j.RESOLUTIONDATE
        ,j2.SUMMARY, j2.CREATED, j2.RESOLUTIONDATE, j3.SUMMARY, j3.CREATED, j3.RESOLUTIONDATE
FROM Jira.jiraissue As j
INNER JOIN Jira.issuelink i
    ON i.SOURCE =j.ID and i.SEQUENCE = 0
INNER JOIN Jira.jiraissue As j2
    ON i.DESTINATION =j2.ID
LEFT JOIN Jira.issuelink i2 
    ON i2.SOURCE = j.ID  and i2.SEQUENCE = 1
LEFT JOIN Jira.jiraissue As j3 
    ON i2.DESTINATION = j3.ID

这很有效。但是当我尝试实现(在内部连接部分之后)到另一个查询时,我收到错误。

  SELECT A.pkey as KREDİ, A.SUMMARY , B.pname AS STATU, D.Sorumlu AS SORUMLU, A.CREATED, A.RESOLUTIONDATE, dbo.CUSTVAL(11931, A.ID, 'S') AS BAYİ, dbo.GetLastStatuTime(A.ID) AS SON_STATU_TAR,j2.SUMMARY, j2.CREATED,j2.UPDATED, j2.RESOLUTIONDATE, j3.SUMMARY, j3.CREATED, j3.UPDATED, j3.RESOLUTIONDATE
FROM Jira.jiraissue A, jira.issuestatus B, AspNetServicesDB.dbo.STATU_MAP D
INNER JOIN Jira.issuelink i
    ON i.SOURCE = A.ID and i.SEQUENCE = 0
INNER JOIN Jira.jiraissue As j2
    ON i.DESTINATION =j2.ID 
LEFT JOIN Jira.issuelink i2 
    ON i2.SOURCE = A.ID  and i2.SEQUENCE = 1
LEFT JOIN Jira.jiraissue As j3 
    ON i2.DESTINATION = j3.ID
WHERE A.issuestatus = B.ID
AND 'BAŞARAN OTOMOTİV' = dbo.CUSTVAL(11931, A.ID, 'S')
AND B.pname = D.JiraStatu collate Turkish_CS_AI
AND A.issuetype != 11

我只将j.ID更改为A.ID。错误消息是;

The multi-part identifier "A.ID" could not be bound.
Msg 4104, Level 16, State 1, Line 8 
The multi-part identifier "A.ID" could not be bound.

我不明白为什么。方法Jira.jiraissue表格有ID列。为什么j.ID在第一次查询中有效,但在第二次查询中无法使用A.ID

1 个答案:

答案 0 :(得分:2)

区别在于您的第二个查询中的多表from子句:

FROM Jira.jiraissue A, jira.issuestatus B, AspNetServicesDB.dbo.STATU_MAP D
INNER JOIN Jira.jiraissue As j2 ON i.DESTINATION =j2.ID 

执行如下:

FROM Jira.jiraissue A, (
    jira.issuestatus B, (
        AspNetServicesDB.dbo.STATU_MAP D
        INNER JOIN Jira.jiraissue As j2 ON i.DESTINATION =j2.ID 
    )
)

在最里层,A未定义,因此您收到错误。