TSQL XML 查询问题
这在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以做到:
this should do it: