外部Where子句中的TSQL内部选择值

时间:2014-11-11 20:45:02

标签: sql sql-server tsql stored-procedures

我有一个问题,我精简到一个非常基本的例子,我想做什么。

我的问题是我在我的工具上创建了一个动态WHERE子句,但是下面的简单选择无法工作。

我有一个名为BS_TrainingEvent_Segments的表,其中包含培训事件。一个事件可以有多个段。我的外部Where子句需要能够同时查询内部和外部选择,但在它所处的位置,它只是说B cannot be bound

SELECT A.[teSegmentID],
       A.[trainingEventID],
       A.[segmentDate],
       A.[nonProdHrs],
      (
             SELECT B.[recordID],
                    B.[segmentID],
                    B.[localeID],
                    B.[teammateCount],
                    B.[leaderCount]
                 FROM dbo.BS_TrainingEvent_SegmentDetails AS B
                 WHERE A.teSegmentID = segmentID
                 FOR    XML PATH ('detail'), TYPE, ELEMENTS, ROOT ('details')
      ) AS B
FROM [red].[dbo].[BS_TrainingEvent_Segments] AS A
WHERE A.[teCategory] = 'Food' AND B.[localeID] = '462'
FOR    XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root')

1 个答案:

答案 0 :(得分:0)

SELECT A.[teSegmentID],
    A.[trainingEventID],
    A.[segmentDate],
    A.[nonProdHrs],
    (
        SELECT B.[recordID],
            B.[segmentID],
            B.[localeID],
            B.[teammateCount],
            B.[leaderCount]
        FROM dbo.BS_TrainingEvent_SegmentDetails AS B
        WHERE A.teSegmentID = B.segmentID AND B.[localeID] = '462'
        FOR XML PATH ('detail'), TYPE, ELEMENTS, ROOT ('details')
    )
FROM [red].[dbo].[BS_TrainingEvent_Segments] AS A
WHERE A.[teCategory] = 'Food'
FOR XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');