如何进行 XQuery - tsql

发布于 2024-12-08 02:32:13 字数 1410 浏览 0 评论 0原文

我得到了这个 XML。

<ArrayOfGuid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <guid>475B07C8-CDEA-4000-BAAE-485336190E10</guid>
     <guid>6E5B87BC-CF80-4AB2-939E-ED951FA604AB</guid>
     <guid>31BFDA60-3BB2-49DC-8C69-10F6E1B540CA</guid>
</ArrayOfGuid>

需要得到类似这样的结果:

BatchGUID
475B07C8-CDEA-4000-BAAE-485336190E10
6E5B87BC-CF80-4AB2-939E-ED951FA604AB
31BFDA60-3BB2-49DC-8C69-10F6E1B540CA

不知何故,我陷入了

DECLARE @BatchGUID XML

SET @BatchGUID = '<ArrayOfGuid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                      <guid>475B07C8-CDEA-4000-BAAE-485336190E10</guid>
                      <guid>6E5B87BC-CF80-4AB2-939E-ED951FA604AB</guid>
                      <guid>31BFDA60-3BB2-49DC-8C69-10F6E1B540CA</guid>
                    </ArrayOfGuid>'

DECLARE @t_xml TABLE
(
    BatchGUID XML
)

INSERT INTO @t_xml
SELECT @BatchGUID

SELECT C.* FROM @t_xml
cross apply (select name.value('guid[1]', 'uniqueidentifier') as [BatchGUID] 
from BatchGUID.nodes('//ArrayOfGuid/guid') as c(name)) as C

This is returned me

BatchGUID
NULL
NULL
NULL

需要知道我在这里做错了什么。

谢谢,

I got this XML.

<ArrayOfGuid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <guid>475B07C8-CDEA-4000-BAAE-485336190E10</guid>
     <guid>6E5B87BC-CF80-4AB2-939E-ED951FA604AB</guid>
     <guid>31BFDA60-3BB2-49DC-8C69-10F6E1B540CA</guid>
</ArrayOfGuid>

Need to get the results like

BatchGUID
475B07C8-CDEA-4000-BAAE-485336190E10
6E5B87BC-CF80-4AB2-939E-ED951FA604AB
31BFDA60-3BB2-49DC-8C69-10F6E1B540CA

Somehow I am stuck with

DECLARE @BatchGUID XML

SET @BatchGUID = '<ArrayOfGuid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                      <guid>475B07C8-CDEA-4000-BAAE-485336190E10</guid>
                      <guid>6E5B87BC-CF80-4AB2-939E-ED951FA604AB</guid>
                      <guid>31BFDA60-3BB2-49DC-8C69-10F6E1B540CA</guid>
                    </ArrayOfGuid>'

DECLARE @t_xml TABLE
(
    BatchGUID XML
)

INSERT INTO @t_xml
SELECT @BatchGUID

SELECT C.* FROM @t_xml
cross apply (select name.value('guid[1]', 'uniqueidentifier') as [BatchGUID] 
from BatchGUID.nodes('//ArrayOfGuid/guid') as c(name)) as C

This is returning me

BatchGUID
NULL
NULL
NULL

Need to know what I'm doing wrong here.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韵柒 2024-12-15 02:32:13

你们是如此非常接近!

您的问题是您正在从 guid 节点中选择 guid[1] 节点的值。当然,guid 节点不包含任何子节点 - 您希望从该节点本身选择值(表示为 .

SELECT C.* FROM @t_xml
cross apply (select name.value('.', 'uniqueidentifier') as [BatchGUID]
from @BatchGUID.nodes('//ArrayOfGuid/guid') as c(name)) as C

You are so very close!

Your problem is that you're selecting the value of the guid[1] node from within the guid node. Of course, the guid node doesn't contain any children - you want to select the value from that node itself (which is expressed as .)

SELECT C.* FROM @t_xml
cross apply (select name.value('.', 'uniqueidentifier') as [BatchGUID]
from @BatchGUID.nodes('//ArrayOfGuid/guid') as c(name)) as C
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文