TSQL XML 查询问题

发布于 2024-10-31 06:21:34 字数 1581 浏览 2 评论 0原文

这在 TSQL 中可能吗?

我通过 xml 参数输入此结构。我需要将其设置到临时表中。

DECLARE @xml xml
SET @xml = '<Events>
            <Event id="8">
                <Responses>
                    <Response id="59">
                        <Loe>
                            <Id>89</Id>
                        </Loe>
                    </Response>
                    <Response id="60">
                        <Loe>
                            <Id>89</Id>
                            <Id>90</Id>
                            <Id>88</Id>
                            <Id>87</Id>
                        </Loe>
                    </Response>
                </Responses>
            </Event>
        </Events>';

尝试将其显示为:

EventId    ResponseId     LoeId
8          59             89
8          60             89
8          60             90
8          60             88
8          60             87

我尝试使用此查询,但它引发错误。

SELECT
  [data].value('../../@id','varchar(100)') AS EventId,
  [data].value('@id','varchar(100)') AS ResponseId,
  [data].value('Loe/Id','varchar(100)') AS LoeId
FROM @xml.nodes('/Events/Event/Responses/Response') as Test([data])

但是,如果我删除 LoeId,它就会起作用,我会得到以下信息:

EventId    ResponseId
8          59             
8          60             

我做错了什么?如何处理查询中的 Loe->Id?

我正在使用 MSSQL 2008。你有什么想法吗?

谢谢。

Is this possible in TSQL?

I am feeding this structure in via an xml param. And I need to set it into a temp table.

DECLARE @xml xml
SET @xml = '<Events>
            <Event id="8">
                <Responses>
                    <Response id="59">
                        <Loe>
                            <Id>89</Id>
                        </Loe>
                    </Response>
                    <Response id="60">
                        <Loe>
                            <Id>89</Id>
                            <Id>90</Id>
                            <Id>88</Id>
                            <Id>87</Id>
                        </Loe>
                    </Response>
                </Responses>
            </Event>
        </Events>';

Trying to display it like:

EventId    ResponseId     LoeId
8          59             89
8          60             89
8          60             90
8          60             88
8          60             87

I tried to use this query, but it throws an error.

SELECT
  [data].value('../../@id','varchar(100)') AS EventId,
  [data].value('@id','varchar(100)') AS ResponseId,
  [data].value('Loe/Id','varchar(100)') AS LoeId
FROM @xml.nodes('/Events/Event/Responses/Response') as Test([data])

But if I remove the LoeId it works and I get this:

EventId    ResponseId
8          59             
8          60             

What I am doing wrong? How do I address the Loe->Id in the query?

I am using MSSQL 2008. Do you have any ideas?

Thanks.

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

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

发布评论

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

评论(1

就此别过 2024-11-07 06:21:34

这应该可以做到:

SELECT
    event.value('@id', 'int') AS Event,
    response.value('@id','int') AS Response,
    id.value('.','int') AS LoeId
FROM
    @xml.nodes('Events/Event') AS t1(event) cross apply
    t1.event.nodes('Responses/Response') AS t2(response) cross apply
    t2.response.nodes('Loe/Id') AS t3(id)
ORDER BY Event, Response, LoeId

this should do it:

SELECT
    event.value('@id', 'int') AS Event,
    response.value('@id','int') AS Response,
    id.value('.','int') AS LoeId
FROM
    @xml.nodes('Events/Event') AS t1(event) cross apply
    t1.event.nodes('Responses/Response') AS t2(response) cross apply
    t2.response.nodes('Loe/Id') AS t3(id)
ORDER BY Event, Response, LoeId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文