TSQL使用外部FROM的内部选择结果

时间:2014-08-27 18:20:04

标签: sql sql-server tsql stored-procedures

我有一个如下所示的查询:

    A.[approvalDate],
    A.[locationID],
    B.[FirstName] + ' ' + B.[LastName] AS nomineeName,
    B.[ntid] AS nomineeNTID,
    B.[qid] AS nomineeQID,
    B.[GeoLocationDesc] as locationName,
    C.[FirstName] + ' ' + C.[LastName] AS submitName,
    C.[ntid] AS submitNTID,
    C.[empID] submitEmpID,
    D.[categoryName],
    (
        SELECT G.[qid] FROM empowermentProxies AS G WHERE G.[proxy] = @QID
    ),
    E.[FirstName] + ' ' + E.[LastName] as behalfOf,
    E.[NTID] AS behalfOfNTID
FROM   empowermentSubmissions AS A
    INNER JOIN
    empTable AS B
    ON A.[nomineeQID] = B.[qid]
    LEFT OUTER JOIN
    empTable AS C
    ON A.[subQID] = C.[qid]
    INNER JOIN
    empowermentCategories AS D
    ON A.[categoryID] = D.[catID]
    JOIN empTable AS E
    ON E.[qid] = G.[qid]
WHERE  @QID IN (SELECT proxy FROM empowermentProxies)
    AND A.[statusID] = 1
FOR        XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');

我正在尝试在join语句中使用内部选择G.qid的结果但是仍然收到错误,指出G没有绑定。

这可能吗?内部选择只返回1个结果。

2 个答案:

答案 0 :(得分:2)

将其移至from子句:

    . . .
    D.[categoryName],
    Q.g_qid,
    . . .
FROM   empowermentSubmissions A INNER JOIN
       empTable B
       ON A.[nomineeQID] = B.[qid] LEFT OUTER JOIN
       empTable C
       ON A.[subQID] = C.[qid] INNER JOIN
       empowermentCategories D
       ON A.[categoryID] = D.[catID] CROSS JOIN
       (SELECT G.[qid] as g_qid
        FROM empowermentProxies G
        WHERE G.[proxy] = @QID
       ) G JOIN
       empTable E
       ON E.[qid] = G.[qid]

答案 1 :(得分:0)

请尝试以下

 A.[approvalDate],
                                   A.[locationID],
                                   B.[FirstName] + ' ' + B.[LastName] AS nomineeName,
                                   B.[ntid] AS nomineeNTID,
                                   B.[qid] AS nomineeQID,
                                   B.[GeoLocationDesc] as locationName,
                                   C.[FirstName] + ' ' + C.[LastName] AS submitName,
                                   C.[ntid] AS submitNTID,
                                   C.[empID] submitEmpID,
                                   D.[categoryName],
                                   sol1.qid,
                                   E.[FirstName] + ' ' + E.[LastName] as behalfOf,
                                   E.[NTID] AS behalfOfNTID
                            FROM   empowermentSubmissions AS A
                                   INNER JOIN
                                   empTable AS B
                                   ON A.[nomineeQID] = B.[qid]
                                   LEFT OUTER JOIN
                                   empTable AS C
                                   ON A.[subQID] = C.[qid]
                                   INNER JOIN
                                   empowermentCategories AS D
                                   ON A.[categoryID] = D.[catID]
                                   JOIN empTable AS E
                                   ON E.[qid] = G.[qid]
    left join (SELECT G1.[qid] FROM empowermentProxies AS G1 WHERE G.[proxy] = @QID) sol1
    on g.qid=sol1.qid



                            WHERE  @QID IN (SELECT proxy FROM empowermentProxies)
                                   AND A.[statusID] = 1
                            FOR    XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');