Sql server 从SQL Server中的XML读取数据
我在从数据库中的XML列读取数据时遇到问题。我需要阅读下面截图显示的数据Sql server 从SQL Server中的XML读取数据,sql-server,xml,Sql Server,Xml,我在从数据库中的XML列读取数据时遇到问题。我需要阅读下面截图显示的数据 <LicenseSeats>47</LicenseSeats> 大家好,这是完整的xml文件 <company:License xmlns:company="urn://schemas.company.com/licensing/license/v1"> <LicenseKey>*****</LicenseKey> <LicenseModel>
<LicenseSeats>47</LicenseSeats>
大家好,这是完整的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>
*****
同时发生的
47
*****
2017-12-14T19:27:35.9051262Z
2020-10-01T04:00:00Z
83e19906-fc53-4187-a258-ae6993873a01
App WMS系统
单位
*******=
******
您的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),请提供可用的示例数据
谢谢。您的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),请提供可用的示例数据
谢谢。试试这个:
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
,请尝试以下操作:
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
否,xmlns:datex
不是默认名称空间。。。可见XML无效,因为根节点位于未声明的公司
命名空间中。。。这里缺少很多东西……事实上,你是对的。不过,我自己并不乐于写出OPs xml,所以希望它们可以用一些有用的数据进行编辑。伙计们,我在第一篇文章中添加了完整的xml,请查看您的第一个解决方案不起作用不,xmlns:datex
不是默认的命名空间。。。可见XML无效,因为根节点位于未声明的公司
命名空间中。。。这里缺少很多东西……事实上,你是对的。然而,我自己并不乐于写出OPs xml,所以希望它们可以用一些有用的数据进行编辑。伙计们,我在第一篇文章中添加了完整的xml,请回顾一下您的第一个解决方案不起作用
节点显然位于xml名称空间中(您的屏幕截图中没有显示)-因此,您需要在查询中包含该名称空间。……这仍然不能是完整的XML。公司:
XML名称空间未在您发布的内容中定义。……我为XML添加了新的更改。
节点显然位于XML名称空间中(您的屏幕截图中未显示该名称空间)-因此,您需要在查询中包含该名称空间…这仍然不能是完整的XML-您发布的内容中没有定义公司:
XML名称空间…我为实际工作的XmlThank man添加了新的更改。我能够得到我需要的结果,我只需将int改为nvarchar,因为该值被设置为nvarchar而不是int。但该查询再次起到了作用,谢谢。@Andresbrian29:如果这个答案帮助您解决了问题,那么请。这将表达你对那些花自己的时间帮助你的人的感激之情。感谢那些真正起作用的人。我能够得到我需要的结果,我只需将int改为nvarchar,因为该值被设置为nvarchar而不是int。但该查询再次起到了作用,谢谢。@Andresbrian29:如果这个答案帮助您解决了问题,那么请。这将表达你对那些花自己的时间帮助你的人的感激之情。