2015-04-08 56 views
0

的導入XML我有一個XML這樣的代碼:有不同數量的節點

<report> 
    <deltagere> 
    <deltager> 
     <number>142555267</number> 
     <date>29-12-2006</date> 
     <name> 
     <name> 
      <from>01-05-2000</from> 
      <to>01-01-2003</to> 
      <text>foo</text> 
     </name> 
     <name> 
      <from>01-01-2003</from> 
      <to>29-12-2006</to> 
      <text>bzz</text> 
     </name> 
     </name> 
     <information> 
     <deltagertype>person</deltagertype> 
     <leader>John Smith</leader> 
     <status>Active</status> 
     </information> 
     <role>Responsible</role> 
    </deltager> 
    <deltager> 
     <number>4000134982</number> 
     <date>05-12-2007</date> 
     <name> 
     <name> 
      <from>07-07-2007</from> 
      <to>05-12-2007</to> 
      <text>bar</text> 
     </name> 
     </name> 
     <information> 
     <deltagertype>person</deltagertype> 
     <leader>Wolfgang Smith</leader> 
     <status>Active</status> 
     </information> 
     <role>Responsible</role> 
    </deltager> 
    ... 
    </deltagere> 
</report> 

其中我進口就好了與此查詢:

SELECT 
    number = deltagere.value('(number)[1]', 'bigint'), 
    dato = deltagere.value('(date)[1]', 'varchar(10)'), 
    nameFrom = XC.value('(from)[1]', 'varchar(10)'), 
    nameTo = XC.value('(to)[1]', 'varchar(10)'), 
    nameText = XC.value('(text)[1]', 'varchar(30)'), 
    deltagertype = deltagere.value('(information/deltagertype)[1]', 'varchar(20)'), 
    leader = deltagere.value('(information/leader)[1]', 'varchar(50)'), 
    deltagerStatus = deltagere.value('(information/status)[1]', 'varchar(50)'), 
deltagerRole = deltagere.value('(role)[1]', 'varchar(50)') 
FROM 
    @XmlFile.nodes('/report/deltagere/deltager') AS XTbl(deltagere) 
CROSS APPLY 
    deltagere.nodes('name/name') AS XT2(XC) 

現在的問題是,不是所有的<deltager>節點有<name>節點。它可能看起來像這樣:

<deltager> 
     <number>1234134982</number> 
     <date>05-12-2007</date> 
     <information> 
     <deltagertype>person</deltagertype> 
     <leader>Wolfgang Smith</leader> 
     <status>Active</status> 
     </information> 
     <role>Responsible</role> 
    </deltager> 

如果缺少名稱節點,我希望該節點的名稱列填充NULL。像這樣:

| number  | dato  | nameFrom | nameTo  | nameText | deltagertype | ... 
-------------+------------+------------+------------+----------+--------------+ ... 
| 1234134982 | 29-12-2006 | NULL  | NULL  | NULL  | person  | ... 

我希望你們中的任何一位專家對如何解決我的問題有一個想法。

+0

看這個[問題](http://stackoverflow.com/questions/63447/how-to-perform-an-if-then-in-an -sql-select),我希望它能幫助你 –

回答

1

您可以使用OUTER APPLY而不是CROSS APPLY。

DECLARE @XmlFile XML 
SET @XmlFile = 
' 
<report> 
    <deltagere> 
    <deltager> 
     <number>142555267</number> 
     <date>29-12-2006</date> 
     <name> 
     <name> 
      <from>01-05-2000</from> 
      <to>01-01-2003</to> 
      <text>foo</text> 
     </name> 
     <name> 
      <from>01-01-2003</from> 
      <to>29-12-2006</to> 
      <text>bzz</text> 
     </name> 
     </name> 
     <information> 
     <deltagertype>person</deltagertype> 
     <leader>John Smith</leader> 
     <status>Active</status> 
     </information> 
     <role>Responsible</role> 
    </deltager> 
    <deltager> 
     <number>4000134982</number> 
     <date>05-12-2007</date> 
     <name> 
     <name> 
      <from>07-07-2007</from> 
      <to>05-12-2007</to> 
      <text>bar</text> 
     </name> 
     </name> 
     <information> 
     <deltagertype>person</deltagertype> 
     <leader>Wolfgang Smith</leader> 
     <status>Active</status> 
     </information> 
     <role>Responsible</role> 
    </deltager> 
    <deltager> 
     <number>1234134982</number> 
     <date>05-12-2007</date> 
     <information> 
     <deltagertype>person</deltagertype> 
     <leader>Wolfgang Smith</leader> 
     <status>Active</status> 
     </information> 
     <role>Responsible</role> 
    </deltager> 
    </deltagere> 
</report> 
' 
SELECT 
    number = deltagere.value('(number)[1]', 'bigint'), 
    dato = deltagere.value('(date)[1]', 'varchar(10)'), 
    nameFrom = XC.value('(from)[1]', 'varchar(10)'), 
    nameTo = XC.value('(to)[1]', 'varchar(10)'), 
    nameText = XC.value('(text)[1]', 'varchar(30)'), 
    deltagertype = deltagere.value('(information/deltagertype)[1]', 'varchar(20)'), 
    leader = deltagere.value('(information/leader)[1]', 'varchar(50)'), 
    deltagerStatus = deltagere.value('(information/status)[1]', 'varchar(50)'), 
deltagerRole = deltagere.value('(role)[1]', 'varchar(50)') 
FROM 
    @XmlFile.nodes('/report/deltagere/deltager') AS XTbl(deltagere) 
OUTER APPLY 
    deltagere.nodes('name/name') AS XT2(XC) 

OUTPUT

number  dato  nameFrom nameTo  nameText deltagertype leader   deltagerStatus deltagerRole 
142555267 29-12-2006 01-05-2000 01-01-2003 foo   person   John Smith  Active   Responsible 
142555267 29-12-2006 01-01-2003 29-12-2006 bzz   person   John Smith  Active   Responsible 
4000134982 05-12-2007 07-07-2007 05-12-2007 bar   person   Wolfgang Smith Active   Responsible 
1234134982 05-12-2007 NULL  NULL  NULL  person   Wolfgang Smith Active   Responsible 
+0

我不知道爲什麼我沒有想到這一點。但它完美的工作,謝謝。 – Kasperhogh

相關問題