2016-01-25 82 views
0

我想弄清楚如何從表中的XML字段的多個節點(實際上是將XML存儲爲文本)獲取多個值。來自XML列的多個值

我見過幾種涉及將XML聲明爲變量並將其用作表格的方法,但我沒有看到這對我有用。 How to Extract data from xml column in sql 2008

我目前使用.value的獲得一些字段,但我不明白如何使它工作,因爲可以有多個LX01_AssignedNumber,我需要從每一個得到所有的ProcedureModifier的。

SELECT CAST(xmldata as xml).value('declare namespace ns1="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/EnrichedMessageXML";declare namespace ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006"; 
(/ns1:X12EnrichedMessage/TransactionSet/ns0:X12_00501_837_P/ns0:TS837_2000A_Loop/ns0:TS837_2000B_Loop/ns0:TS837_2300_Loop/ns0:TS837_2400_Loop/ns0:SV1_ProfessionalService/ns0:C003_CompositeMedicalProcedureIdentifier/C00303_ProcedureModifier) [1]', 'varchar(20)') AS RendAttendNPI 
FROM EDI_DATA 

我如何從每個記錄所有的行號和所有程序修改器

XML:

<ns1:X12EnrichedMessage xmlns:ns1="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/EnrichedMessageXML"> 
... 
    <TransactionSet> 
     <!-- ProcessLogID=PLG0005169955 ;ProcessLogDetailID=PLG0005173285 ;EnvID=1;RetryCount=1 --> 
     <ns0:X12_00501_837_P xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006"> 
      <ns0:TS837_2000A_Loop xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006"> 
       <ns0:TS837_2000B_Loop xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006"> 
        <ns0:TS837_2300_Loop xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006"> 
         <ns0:TS837_2400_Loop> 
          <ns0:LX_ServiceLineNumber> 
           <LX01_AssignedNumber>1</LX01_AssignedNumber> 
          </ns0:LX_ServiceLineNumber> 
          <ns0:SV1_ProfessionalService> 
           <ns0:C003_CompositeMedicalProcedureIdentifier> 
            <C00301_ProductorServiceIDQualifier>HC</C00301_ProductorServiceIDQualifier> 
            <C00302_ProcedureCode>26340</C00302_ProcedureCode> 
            <C00303_ProcedureModifier>AG</C00303_ProcedureModifier> 
            <C00304_ProcedureModifier>58</C00304_ProcedureModifier> 
            <C00305_ProcedureModifier>51</C00305_ProcedureModifier> 
            <C00306_ProcedureModifier>XS</C00306_ProcedureModifier> 
           </ns0:C003_CompositeMedicalProcedureIdentifier> 
           <SV102_LineItemChargeAmount>8918</SV102_LineItemChargeAmount> 
           <SV103_UnitorBasisforMeasurementCode>UN</SV103_UnitorBasisforMeasurementCode> 
           <SV104_ServiceUnitCount>13</SV104_ServiceUnitCount> 
           <ns0:C004_CompositeDiagnosisCodePointer> 
            <C00401_DiagnosisCodePointer>1</C00401_DiagnosisCodePointer> 
            <C00402_DiagnosisCodePointer>2</C00402_DiagnosisCodePointer> 
           </ns0:C004_CompositeDiagnosisCodePointer> 
          </ns0:SV1_ProfessionalService> 
          <ns0:DTP_SubLoop_2> 
           <ns0:DTP_Date_ServiceDate> 
            <DTP01_DateTimeQualifier>472</DTP01_DateTimeQualifier> 
            <DTP02_DateTimePeriodFormatQualifier>D8</DTP02_DateTimePeriodFormatQualifier> 
            <DTP03_ServiceDate>20160104</DTP03_ServiceDate> 
           </ns0:DTP_Date_ServiceDate> 
          </ns0:DTP_SubLoop_2> 
          <ns0:REF_SubLoop_7> 
           <ns0:REF_LineItemControlNumber> 
            <REF01_ReferenceIdentificationQualifier>6R</REF01_ReferenceIdentificationQualifier> 
            <REF02_LineItemControlNumber>11453481</REF02_LineItemControlNumber> 
           </ns0:REF_LineItemControlNumber> 
          </ns0:REF_SubLoop_7> 
         </ns0:TS837_2400_Loop> 
         <ns0:TS837_2400_Loop> 
          <ns0:LX_ServiceLineNumber> 
           <LX01_AssignedNumber>2</LX01_AssignedNumber> 
          </ns0:LX_ServiceLineNumber> 
          <ns0:SV1_ProfessionalService> 
           <ns0:C003_CompositeMedicalProcedureIdentifier> 
            <C00301_ProductorServiceIDQualifier>HC</C00301_ProductorServiceIDQualifier> 
            <C00302_ProcedureCode>20680</C00302_ProcedureCode> 
            <C00303_ProcedureModifier>58</C00303_ProcedureModifier> 
           </ns0:C003_CompositeMedicalProcedureIdentifier> 
           <SV102_LineItemChargeAmount>1277</SV102_LineItemChargeAmount> 
           <SV103_UnitorBasisforMeasurementCode>UN</SV103_UnitorBasisforMeasurementCode> 
           <SV104_ServiceUnitCount>1</SV104_ServiceUnitCount> 
           <ns0:C004_CompositeDiagnosisCodePointer> 
            <C00401_DiagnosisCodePointer>3</C00401_DiagnosisCodePointer> 
           </ns0:C004_CompositeDiagnosisCodePointer> 
          </ns0:SV1_ProfessionalService> 
         </ns0:TS837_2400_Loop> 
        </ns0:TS837_2300_Loop> 
       </ns0:TS837_2000B_Loop> 
      </ns0:TS837_2000A_Loop> 
     </ns0:X12_00501_837_P> 
    </TransactionSet> 
</ns1:X12EnrichedMessage> 

回答

1

查找到SQL Server CROSS APPLY,你可以用它來切碎單XML數據分成多個行,例如:

;WITH XMLNAMESPACES ('http://schemas.microsoft.com/BizTalk/EDI/X12/2006' as ns0 
        ,'http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/EnrichedMessageXML' as ns1) 

SELECT 
    TS837_2400_Loop.value('(.//LX01_AssignedNumber)[1]', 'int') 'line_number' 
    ,C00303_ProcedureModifier.value('.', 'varchar(100)') 'procedure_modifier' 
FROM EDI_DATA 
    CROSS APPLY (select CONVERT(XML, xmldata)) as P(X) 
    CROSS APPLY X.nodes('.//ns0:TS837_2400_Loop') AS Q(TS837_2400_Loop) 
    CROSS APPLY TS837_2400_Loop.nodes('.//C00303_ProcedureModifier') AS R(C00303_ProcedureModifier) 

sqlfiddle demo

出來放:

| line_number | procedure_modifier | 
|-------------|--------------------| 
|   1 |     AG | 
|   2 |     58 | 
+0

啊 - 那看上去更像是我想要比其他XML解析我已經找到。謝謝。我會盡量在接下來的幾天內完成這項工作。過去兩天我被困在了陪審團的職責中,現在一切都落後了。 –

+0

嘿har07 - 有沒有辦法讓程序代碼(C0030 ** x ** _ ProcedureModifier)動態?可以有五個。或者是硬編碼它們的唯一方法,如果它們不存在,它們將是NULL? –

+0

謝謝 - 現在運行良好。我希望我可以使程序代碼具有動態性,但由於每個代碼具有不同的名稱,所以這可能是不可能的。 –