2011-04-08 4 views
1

내가 뭘 하려는지 꽤 쉽게 생각할 수 있습니다. SQL 서버에서 FOR XML에 익숙하다면 아래쪽으로 건너 뛰고 굵은 텍스트 을 읽는 것이 좋습니다. 부분 :)SQL FOR XML 쿼리를 특별히 명명 된 부모 요소로 분할

나는 결과를 얻기 위해 SQL Server 2005에서 FOR XML 문을 사용하려고합니다. 현재 내가이가 ... 당신이 볼 수 있듯이, Reason 요소와 PulledSupportReason 요소가 같은 테이블에서 오는 다음과 같은 XML을 반환

SELECT 
    txtReasonTypeID AS [ReasonTypeID] 
    , 
    (SELECT 
    [Reason].intReasonID, 
    [Reason].txtReason 
    FROM CST_lnkProfileReason INNER JOIN 
    CST_tblReason AS [Reason] ON CST_lnkProfileReason.intReasonID = [Reason].intReasonID 
    WHERE CST_lnkProfileReason.intProfileID = @intProfileID 
    AND CST_lnkProfileReason.txtReasonTypeID = [Response].txtReasonTypeID 
    ORDER BY Reason.txtReason 
    FOR XML AUTO, TYPE 
    ) 
    , 
    (SELECT 
    [PulledSupportReason].intReasonID, 
    [PulledSupportReason].txtReason 
    FROM CST_lnkPulledSupportReason INNER JOIN 
    CST_tblReason AS [PulledSupportReason] ON CST_lnkPulledSupportReason.intReasonID = [PulledSupportReason].intReasonID 
    WHERE CST_lnkPulledSupportReason.intProfileID = @intProfileID 
    AND CST_lnkPulledSupportReason.txtReasonTypeID = [Response].txtReasonTypeID 
    ORDER BY [PulledSupportReason].txtReason 
    FOR XML AUTO, TYPE 
    ) 
FROM CST_tblReasonTypes AS [Response] 
FOR XML AUTO, ROOT('ResponseProfile') 

...

<ResponseProfile> 
    <Response ReasonTypeID="ExampleType"> 
    <Reason intReasonID="106" txtReason="Call Back - 1"/> 
    <Reason intReasonID="147" txtReason="Call Back - 2"/> 
    <PulledSupportReason intReasonID="892" txtReason="PS Reason a"/> 
    <PulledSupportReason intReasonID="893" txtReason="PS Reason b"/> 
    </Response> 
    ...more <Response>s 
</ResponseProfile> 

그들은 분리되어 있지만, 이 쿼리의 요소 (아마 나쁜 디자인의 경우)하지만 - 제가 원하는 것은 간단하다 , 내가 XML PATH를 사용하여이를 달성 할 수 있다고 생각하거나 ... ReasonPulledSupportReason 요소 예를 들어 주위

<ResponseProfile> 
    <Response ReasonTypeID="ExampleType"> 
    <Reasons> 
     <Reason intReasonID="106" txtReason="Call Back - 1"/> 
     <Reason intReasonID="147" txtReason="Call Back - 2"/> 
    </Reasons> 
    <PulledSupportReasons> 
     <PulledSupportReason intReasonID="892" txtReason="PS Reason a"/> 
     <PulledSupportReason intReasonID="893" txtReason="PS Reason b"/> 
    </PulledSupportReasons> 
    </Response> 
    ...more <Response>s 
</ResponseProfile> 

를 부모 요소를 넣어 XML EXPLICIT? 어떤 도움을 주셔서 감사합니다 :)

답변

2

FOR XML PATH('....'), ROOT('....')을 사용해보십시오 - 당신이 찾고있는 것을 얻을 수 있어야합니다.

내가 두 번째 부속이를 설명하고 있습니다 - 첫번째로 하나 그에 따라 적응이 작동합니다

(SELECT 
    [PulledSupportReason].intReasonID, 
    [PulledSupportReason].txtReason 
FROM  
    CST_lnkPulledSupportReason 
INNER JOIN 
    CST_tblReason AS [PulledSupportReason] ON CST_lnkPulledSupportReason.intReasonID = [PulledSupportReason].intReasonID 
WHERE 
    CST_lnkPulledSupportReason.intProfileID = @intProfileID 
    AND CST_lnkPulledSupportReason.txtReasonTypeID = [Response].txtReasonTypeID 
ORDER BY 
    [PulledSupportReason].txtReason 
FOR XML PATH('PulledSupportReason'), TYPE 
) AS 'PulledSupportReasons' 

, 나는 희망을! 선택 전체 서브 별칭을 추가

내부 FOR XML PATH(PulledSupportReason') 사용하기에 가장 안쪽의 XML 태그를 정의합니다 (AS 'PulledSupportReasons')을 (내가 볼을 손에서 테이블을 가지고 있지 않기 때문에 정말 테스트 할 수 없습니다)이 그 부속 랩핑을 제공합니다 정의 된 별명과 동일한 XML 태그.

+1

나는 쉽게 알 수있었습니다! 당신은 위대한 전설입니다! 그러나 각 'SELECT'뒤에 'AS'PulledSupportReasons' 등의 부분을 추가하기 만하면됩니다. 필자는'XML PATH'를 사용할 필요가 없었습니다 - 각 PulledSupportReason 행을''태그 안에 넣고 싶었던 결과를 얻었습니다. 날 따라와 줘! 감사 : D –

+0

또한'XML PATH' 옵션은 'intReasonID','txtReason'과 같은 행 속성을' 106 <\ intReasonID> '와 같은 별도의 요소로 구분하는 효과가있었습니다. 아직도이 'XML for 비즈니스'에서 머리를 맞으려고 노력하고 있습니다! –

+0

@El Ronnoco : 'FOR XML PATH'에서 속성으로 선택하고자하는 경우 @로 시작하는 열 별칭을 추가하십시오 :'SELECT PulledSupportReason.intReasonID AS'@ ReasonID'' –

관련 문제