0

为什么这个查询不起作用?

SELECT ROW_NUMBER() OVER (
        ORDER BY DocumentID
        ) peta_rn
    , d.DocumentID
    , d.IsReEfiled
    , d.IGroupID
    , d.ITypeID
    , d.RecordingDateTime
    , dbo.GetLatestStatusDateTime(d.DocumentID) AS LatestStatusDatetime
    , dbo.FnCanChangeDocumentStatus(d.DocumentStatusID, d.DocumentID) AS CanChangeStatus
    , d.IDate
    , d.InstrumentID
    , d.DocumentStatusID
    , ig.Abbreviation AS IGroupAbbreviation
    , u.Username
    , j.JDAbbreviation
    , inf.DocumentName
    , it.Abbreviation AS ITypeAbbreviation
    , d.DocumentDate
    , ds.Abbreviation AS DocumentStatusAbbreviation
    , dbo.GetFlatDocumentName(d.DocumentID) AS FlatDocumentName
FROM Documents d
INNER JOIN IGroupes ig
    ON d.IGroupID = ig.IGroupID
LEFT JOIN ITypes it
    ON d.ITypeID = it.ITypeID
LEFT JOIN Users u
    ON u.UserID = d.UserID
LEFT JOIN DocumentStatuses ds
    ON d.DocumentStatusID = ds.DocumentStatusID
LEFT JOIN InstrumentFiles inf
    ON d.DocumentID = inf.DocumentID
INNER JOIN Jurisdictions j
    ON j.JurisdictionID = d.JurisdictionID
WHERE 1 = 1

而这个有效:

SELECT ROW_NUMBER() OVER (
        ORDER BY d.DocumentID
        ) peta_rn
    , d.DocumentID
    , d.IsReEfiled
    , d.IGroupID
    , d.ITypeID
    , d.RecordingDateTime
    , dbo.GetLatestStatusDateTime(d.DocumentID) AS LatestStatusDatetime
    , dbo.FnCanChangeDocumentStatus(d.DocumentStatusID, d.DocumentID) AS CanChangeStatus
    , d.IDate
    , d.InstrumentID
    , d.DocumentStatusID
    , ig.Abbreviation AS IGroupAbbreviation
    , u.Username
    , j.JDAbbreviation
    , inf.DocumentName
    , it.Abbreviation AS ITypeAbbreviation
    , d.DocumentDate
    , ds.Abbreviation AS DocumentStatusAbbreviation
    , dbo.GetFlatDocumentName(d.DocumentID) AS FlatDocumentName
FROM Documents d
INNER JOIN IGroupes ig
    ON d.IGroupID = ig.IGroupID
LEFT JOIN ITypes it
    ON d.ITypeID = it.ITypeID
LEFT JOIN Users u
    ON u.UserID = d.UserID
LEFT JOIN DocumentStatuses ds
    ON d.DocumentStatusID = ds.DocumentStatusID
LEFT JOIN InstrumentFiles inf
    ON d.DocumentID = inf.DocumentID
INNER JOIN Jurisdictions j
    ON j.JurisdictionID = d.JurisdictionID
WHERE 1 = 1

如您所见,唯一的区别是我指定了 d.DocumentID。无论如何我可以避免指定d.DocumentID,而是只指定DocumentID而不使用额外的选择?因为毕竟所有 documentID 都引用同一个值。

4

2 回答 2

4

问题在于这DocumentID两个表InstrumentFilesDocuments,那么查询引擎如何知道您指的是哪一个?

所以简而言之,,您必须d.DocumentID在这种情况下指定。

于 2012-10-17T04:09:35.780 回答
2

不。

您应该提供别名d,因为多个表包含DocumentID. 引擎对应该在哪里(或哪个表)执行订单感到困惑。

于 2012-10-17T04:10:09.387 回答