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

问题描述 投票:0回答:1

我有 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>
sql sql-server xml sql-server-2008 t-sql
1个回答
1
投票

我尝试快速拍摄:

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 仅有一条记录...

© www.soinside.com 2019 - 2024. All rights reserved.