使用SQL从XML数组中提取数据

时间:2018-08-14 12:46:18

标签: sql sql-server xml tsql

我有以下XML,并希望从中提取PrimaryTeam,SecondaryTeams和OverflowTeams数组,并用逗号分隔或每行一个。

我有以下xml:

MLPClassifier

然后我创建了以下SQL语句以尝试提取团队

declare @xml xml 
set @xml = '<SimpleStrategy xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Synthesys.Switch.ACD">
  <Id>00000000-0000-0000-0000-000000000000</Id>
  <Name>Default</Name>
  <AcceptedCLIs xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:string>07811353995</d2p1:string>
  </AcceptedCLIs>
  <ActiveHours>
    <FridayEnd />
    <FridayStart />
    <MondayEnd />
    <MondayStart />
    <SaturdayEnd />
    <SaturdayStart />
    <SundayEnd />
    <SundayStart />
    <ThursdayEnd />
    <ThursdayStart />
    <TuesdayEnd />
    <TuesdayStart />
    <UseIndividualWeekDays>false</UseIndividualWeekDays>
    <WednesdayEnd />
    <WednesdayStart />
    <WeekdayEnd />
    <WeekdayStart />
  </ActiveHours>
  <AgentUserName />
  <AllowRouteDuringFinalMessage>false</AllowRouteDuringFinalMessage>
  <CRMPrefix />
  <DirectDDIMessage />
  <DirectDDIPassThrough>false</DirectDDIPassThrough>
  <EmergencyBusyBack>false</EmergencyBusyBack>
  <EmergencyDivertNumber />
  <EmergencyWavFile />
  <FinallyDivertNumber />
  <FinallyDrop>true</FinallyDrop>
  <FinallyMessageFile />
  <MaximumQueueLength>0</MaximumQueueLength>
  <MaximumQueueWait>0</MaximumQueueWait>
  <MinimumRingTime>4000</MinimumRingTime>
  <MusicOnHold />
  <MusicWhileWaiting />
  <NumberOfRings>2</NumberOfRings>
  <OutOfHoursDivertNumber />
  <OutOfHoursDrop>true</OutOfHoursDrop>
  <OutOfHoursMessage />
  <OverflowMessage />
  <OverflowTeams xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
  <PrimaryTeams xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:int>3</d2p1:int>
    <d2p1:int>1</d2p1:int>
  </PrimaryTeams>
  <Priority>1</Priority>
  <RecordAgent>false</RecordAgent>
  <RecordCall>true</RecordCall>
  <RecordCustomer>false</RecordCustomer>
  <RegulatoryMessage>Default.wav</RegulatoryMessage>
  <SecondaryOverflowMessage />
  <SecondaryTeams xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
  <SendBusyIfQueueTooLong>false</SendBusyIfQueueTooLong>
  <SendBusyIfWaitTooLong>false</SendBusyIfWaitTooLong>
  <TimeInOverflow>-1</TimeInOverflow>
  <TimeWithDirectDDI>20000</TimeWithDirectDDI>
  <TimeWithPrimaryTeams>-1</TimeWithPrimaryTeams>
  <TimeWithSecondaryTeams>20000</TimeWithSecondaryTeams>
  <UseDirectDDI>false</UseDirectDDI>
  <UsePAM>false</UsePAM>
  <UseSecondaryTeams>false</UseSecondaryTeams>
  <WrapTime>40000</WrapTime>
</SimpleStrategy>'

但是我得到的只是将TeamID连接在一起。

,主要团队,次要团队,溢出团队

默认值31,0,0

有什么想法吗?

谢谢

马特

1 个答案:

答案 0 :(得分:0)

您的XML在<PrimaryTeams>中显示了两个团队ID,而其他两个团队节点都为空...您没有告诉我们那里的预期数量。但是,以下方法将返回一种实体-值对,其中包含所有团队的所有ID。希望这是您需要的:

;WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as i, 'http://schemas.microsoft.com/2003/10/Serialization/Arrays' as d2p1,
                     DEFAULT 'http://schemas.datacontract.org/2004/07/Synthesys.Switch.ACD')

SELECT 'Name' AS Caption
      ,1 AS RowInx
      ,@xml.value('(/SimpleStrategy/Name)[1]', 'varchar(255)') AS Content
UNION ALL 
SELECT 'Primary Team'
      ,ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
      ,t.value('.','varchar(255)')
FROM @xml.nodes('/SimpleStrategy/PrimaryTeams/d2p1:int') A(t)
UNION ALL 
SELECT 'Secondary Team'
      ,ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
      ,t.value('.','varchar(255)')
FROM @xml.nodes('/SimpleStrategy/SecondaryTeams/d2p1:int') A(t)
UNION ALL 
SELECT 'Overflow-Team'
      ,ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
      ,t.value('.','varchar(255)')
FROM @xml.nodes('/SimpleStrategy/OverflowTeams/d2p1:int') A(t);