使用TSQL和For XML Path生成XML输出

时间:2017-03-21 16:11:43

标签: sql sql-server xml sql-server-2008 tsql

我有3个临时表,全部由3个独立查询填充,并且彼此关联,具有1对1的关系,这些表是DemographicRecord,GPRegistrationDetails,MaternityBookingDetails。所有3列之间的列不同,但每个列共享PatientID键。我的问题是使用XML Path如何按照以下格式从3个相关数据集输出XML。

    <MAT001MothersDemographics>
        <LocalPatientIdMother>BLANKED</LocalPatientIdMother>
        <OrgCodeLocalPatientIdMother>BLANKED</OrgCodeLocalPatientIdMother>
        <OrgCodeRes>BLANKED</OrgCodeRes>
        <NHSNumberMother>BLANKED</NHSNumberMother>
        <NHSNumberStatusMother>BLANKED</NHSNumberStatusMother>
        <PersonBirthDateMother>BLANKED</PersonBirthDateMother>
        <Postcode>BLANKED</Postcode>
        <EthnicCategoryMother>BLANKED</EthnicCategoryMother>
        <PersonDeathDateTimeMother>BLANKED</PersonDeathDateTimeMother>
        <MAT003GPPracticeRegistration>
            <LocalPatientIdMother>BLANKED</LocalPatientIdMother>
            <OrgCodeGMPMother>BLANKED</OrgCodeGMPMother>
            <StartDateGMPRegistration>BLANKED</StartDateGMPRegistration>
            <EndDateGMPRegistration>BLANKED</EndDateGMPRegistration>
            <OrgCodeCommissioner>BLANKED</OrgCodeCommissioner>
        </MAT003GPPracticeRegistration>
        <MAT101BookingAppointmentDetails>
            <AntenatalAppDate>BLANKED</AntenatalAppDate>
            <LocalPatientIdMother>BLANKED</LocalPatientIdMother>
            <EDDAgreed>BLANKED</EDDAgreed>
            <EDDMethodAgreed>BLANKED</EDDMethodAgreed>
            <PregnancyFirstContactDate>BLANKED</PregnancyFirstContactDate>
            <PregnancyFirstContactCareProfessionalType>BLANKED</PregnancyFirstContactCareProfessionalType>
            <LastMenstrualPeriodDate>BLANKED</LastMenstrualPeriodDate>
            <PhysicalDisabilityStatusIndMother>BLANKED</PhysicalDisabilityStatusIndMother>
            <FirstLanguageEnglishIndMother>BLANKED</FirstLanguageEnglishIndMother>
            <EmploymentStatusMother>BLANKED</EmploymentStatusMother>
            <SupportStatusMother>BLANKED</SupportStatusMother>
            <EmploymentStatusPartner>BLANKED</EmploymentStatusPartner>
            <PreviousCaesareanSections>BLANKED</PreviousCaesareanSections>
            <PreviousLiveBirths>BLANKED</PreviousLiveBirths>
            <PreviousStillBirths>BLANKED</PreviousStillBirths>
            <PreviousLossesLessThan24Weeks>BLANKED</PreviousLossesLessThan24Weeks>
            <SubstanceUseStatus>BLANKED</SubstanceUseStatus>
            <SmokingStatus>BLANKED</SmokingStatus>
            <CigarettesPerDay>BLANKED</CigarettesPerDay>
            <AlcoholUnitsPerWeek>BLANKED</AlcoholUnitsPerWeek>
            <FolicAcidSupplement>BLANKED</FolicAcidSupplement>
            <MHPredictionDetectionIndMother>BLANKED</MHPredictionDetectionIndMother>
            <PersonWeight>BLANKED</PersonWeight>
            <PersonHeight>BLANKED</PersonHeight>
            <ComplexSocialFactorsInd>BLANKED</ComplexSocialFactorsInd>
        </MAT101BookingAppointmentDetails>
</MAT001MothersDemographics>

到目前为止,我已经尝试过:

SELECT
(SELECT * FROM #temp2 
JOIN #temp ON #temp2.LocalPatientIdMother = #temp.LocalPatientIdMother
JOIN #temp3 ON #temp2.LocalPatientIdMother = #temp3.LocalPatientIdMother
FOR XML PATH('MAT001'), TYPE) AS 'MAT001MothersDemographics' 
FOR XML PATH(''), ROOT('root')

但这不是正确的形状,有人可以建议我如何有效地使用TSQL和FOR XML PATH,以便生成上述输出吗?在显示其他数据之前,我目前正在为每条记录重复人口统计数据?

    <MAT001MothersDemographics>
     <MAT001>
      <LocalPatientIdMother>BLANKED</LocalPatientIdMother>
      <OrgCodeLocalPatientIdMother>BLANKED</OrgCodeLocalPatientIdMother>
      <OrgCodeRes>BLANKED</OrgCodeRes>
      <NHSNumberMother>BLANKED</NHSNumberMother>
      <NHSNumberStatusMother>BLANKED</NHSNumberStatusMother>
      <PersonBirthDateMother>BLANKED</PersonBirthDateMother>
      <Postcode>BLANKED</Postcode>
      <EthnicCategoryMother>BLANKED</EthnicCategoryMother>
      <PersonDeathDateTimeMother>BLANKED</PersonDeathDateTimeMother>
      </MAT001>
    </MAT001MothersDemographics>
    <MAT001MothersDemographics>
     <MAT001>
      <LocalPatientIdMother>BLANKED</LocalPatientIdMother>
      <OrgCodeLocalPatientIdMother>BLANKED</OrgCodeLocalPatientIdMother>
      <OrgCodeRes>BLANKED</OrgCodeRes>
      <NHSNumberMother>BLANKED</NHSNumberMother>
      <NHSNumberStatusMother>BLANKED</NHSNumberStatusMother>
      <PersonBirthDateMother>BLANKED</PersonBirthDateMother>
      <Postcode>BLANKED</Postcode>
      <EthnicCategoryMother>BLANKED</EthnicCategoryMother>
      <PersonDeathDateTimeMother>BLANKED</PersonDeathDateTimeMother>
      </MAT001>
    </MAT001MothersDemographics>

非常感谢

1 个答案:

答案 0 :(得分:1)

我必须承认,你的问题很不清楚......你发布了很多不需要的细节(例如大XML),但是你没有提供表格结构和样本数据等必要的信息。对于将来,请阅读How to ask a good SQL questionHow to create a MCVE

但是 - 我的魔法水晶球从清洁回来了! - 我尝试快速拍摄:

SELECT t.*
      ,(
         SELECT * 
         FROM #temp2 AS t2 
         WHERE t.LocalPatientIdMother=t2.LocalPatientIdMother
         FOR XML PATH('MAT003GPPracticeRegistration'),TYPE
       ) AS [*]
      ,(
         SELECT * 
         FROM #temp3 AS t3 
         WHERE t.LocalPatientIdMother=t3.LocalPatientIdMother
         FOR XML PATH('MAT101BookingAppointmentDetail'),TYPE
       ) AS [*]
FROM #temp AS t
FOR XML PATH('MAT001MothersDemographics');

这将返回#temp1的所有列,并将嵌套#temp2#temp3的相关行。这是基于这样的假设,即每个表中只有一条给定ID的记录......