繁体   English   中英

解析 SQL 服务器表中的 XML 数据

[英]Parse XML data in SQL Server table

我正在研究 xml 解析,使用我在 stackoverflow 中找到的示例,但我遇到了疑问。 如果我有这样的事情

DECLARE @xml xml
SET @xml = 
'<?xml version="1.0" encoding="UTF-8"?> 
<oo_outbound_order>   
         <oo_master>
              <Code>123</Code>
              <Name>Branan</Name> 
         </oo_master> 
    </oo_outbound_order>'

SELECT 
    n.value('(./Code/text())[1]','int') as CODE
 , n.value('(./Name/text())[1]','Varchar(50)') as NAME
FROM @xml.nodes('/oo_outbound_order/oo_master') as a(n)

我得到这样的东西

CODE    NAME
123     Branan

但如果我想将其中一个字段存储为 xml

<?xml version="1.0" encoding="UTF-8"?> 
<oo_outbound_order> 
        <Active>true</Active>
        <Weight>0.02</Weight>
         <oo_master>
              <Code>123</Code>
              <Name>Branan</Name> 
        </oo_master>
    </oo_outbound_order> 

我想得到这样的东西

 Active   Weight    ColWithXML
  true     0.02     <oo_master><Code>123</Code><Name>Branan</Name></oo_master>


SELECT 
    n.value('Active[1][not(@xsi:nil = "true")]', 'BIT') as Active
 , n.value('Weight[1][not(@xsi:nil = "true")]', 'DECIMAL(29,5)') as Weight
, ??????????????????

我怎样才能做到这一点? 谢谢

查询()方法

小提琴

DECLARE @xml xml;

select @xml ='<?xml version="1.0" encoding="UTF-8"?> 
<oo_outbound_order> 
    <Active>true</Active>
    <Weight>0.02</Weight>
     <oo_master>
          <Code>123</Code>
          <Name>Branan</Name> 
    </oo_master>
</oo_outbound_order>';

SELECT 
   n.value('Active[1][not(@xsi:nil = "true")]', 'BIT') as Active
 , n.value('Weight[1][not(@xsi:nil = "true")]', 'DECIMAL(29,5)') as Weight
 , a.n.query('./oo_master') as explicitcurrentnode_master
 , a.n.query('oo_master') as impliedcurrentnode_master
 , a.n.query('./Active') as Activexml
 , a.n.query('./oo_master/Name') as master_Namexml
FROM @xml.nodes('/oo_outbound_order') as a(n);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM