SQL xml查询返回null

时间:2014-03-28 00:34:24

标签: sql xml

我有以下XML:

<Matter> 
  <CriticalDates>
    <CriticalDate>
      <CriticalDateId>2</CriticalDateId>
      <Name>Instruction Date</Name>     
      <Value_FieldId>9F21</Value_FieldId>
      <Confirmed_FieldId />
      <Status>In Progress</Status>
      <ConfirmStatus />
      <CompPercent>0</CompPercent>
      <Order>0</Order>
      <Value>2014-03-28T06:00:00+11:00</Value>
      <Confirmed>false</Confirmed>
    </CriticalDate>
    <CriticalDate>
      <CriticalDateId>-2</CriticalDateId>
      <Name>Completion Date</Name>
      <Value_FieldId>9F22</Value_FieldId>
      <Confirmed_FieldId>9F27</Confirmed_FieldId>
      <Status>Complete</Status>
      <ConfirmStatus />
      <CompPercent>0</CompPercent>
      <Order>1</Order>
      <Value />
      <Confirmed>false</Confirmed>
    </CriticalDate>
    <CriticalDate>
      <CriticalDateId>-3</CriticalDateId>
      <Name>Not Proceeding Date</Name>
      <Value_FieldId>9F23</Value_FieldId>
      <Confirmed_FieldId />
      <Status>Not Proceeding</Status>
      <ConfirmStatus />
      <CompPercent>0</CompPercent>
      <Order>2</Order>
      <Value />
      <Confirmed>false</Confirmed>
    </CriticalDate>
  </CriticalDates>
</Matter>

使用以下方法选择所有节点作为行

SELECT
  MatterId,
  MatterXml,
  MD.CD.value('(Name)[1]', 'VARCHAR(50)') AS 'Name', 
  MD.CD.value('(Status)[1]', 'VARCHAR(50)') AS 'Status', 
  MD.CD.value('(value)[1]', 'DATE') AS 'CriticalDate', 
  MD.CD.value('(Confirmed)[1]', 'VARCHAR(50)') AS 'Confirmed'
FROM
  dbo.Matter m 
  CROSS APPLY m.MatterXml.nodes('/Matter/CriticalDates/CriticalDate') AS MD(CD)

当我运行这个时,我得到3行,但是当XML中有日期时,所有CriticalDates都返回NULL,即使是第一个。请帮忙!

1 个答案:

答案 0 :(得分:0)

XML区分大小写。尝试是:

MD.CD.value('(Value)[1]', 'DATE') AS 'CriticalDate',