从SQL Server中的XML读取数据

时间:2017-12-15 16:22:39

标签: sql-server xml

我在从数据库中的XML列读取数据时遇到问题。我需要阅读下面屏幕截图中的数据

<LicenseSeats>47</LicenseSeats>

enter image description here

这是我正在使用的代码,但是当我运行它时,它返回NULL。

SELECT                      
    T.C.value('LicenseSeats[47]', 'int') AS LicenseSeats
FROM
    table_license.Licenses
CROSS APPLY                 
    LicenseFile.nodes('//LicenseSeats') AS T(C)

你好,伙计们这里是完整的xml文件

<company:License xmlns:company="urn://schemas.company.com/licensing/license/v1">
  <LicenseKey>*****</LicenseKey>
  <LicenseModel>Concurrent</LicenseModel>
  <LicenseSeats>47</LicenseSeats>
  <HardwareKey>*****</HardwareKey>
  <GeneratedOn>2017-12-14T19:27:35.9051262Z</GeneratedOn>
  <ExpiresOn>2020-10-01T04:00:00Z</ExpiresOn>
  <ProductId>83e19906-fc53-4187-a258-ae6993873a01</ProductId>
  <ProductName>App WMS System</ProductName>
  <CustomerName>Company</CustomerName>
  <Features>
    <Feature id="*******" name="3PL Management" />
    <Feature id="*******" name="Manufacturing" />
    <Feature id="*******" name="Yard Management" />
  </Features>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
      <Reference URI="">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
        <DigestValue>*******=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>******</SignatureValue>
  </Signature>
</company:License>

2 个答案:

答案 0 :(得分:1)

您的XML中有一个命名空间,您需要让SQL Server了解它。

未经测试,因为您提供的是图像而不是实际的xml,但是:

WITH XMLNAMESPACES (DEFAULT 'urn://schema.company.com/licensing/licence/v1')
SELECT T.C.value('LicenseSeats[47]', 'int') AS LicenseSeats
FROM table_license.Licenses
     CROSS APPLY LicenseFile.nodes('//LicenseSeats') AS T(C);

如果这不起作用(并不是因为我错过了输入uri),请提供耗材样本数据。

感谢。

答案 1 :(得分:1)

试试这个:

WITH XMLNAMESPACES('urn://schemas.company.com/licensing/license/v1' AS cpy)
SELECT
    LicenseSeats = XC.value('(LicenseSeats)[1]', 'int')
FROM
    table_license.Licenses
CROSS APPLY                 
    LicenseFile.nodes('/cpy:License') AS XT(XC)

这应该从XML的47元素中获取值<LicenseSeats>