简体   繁体   English

Sql Server 2008粉碎XML

[英]Sql Server 2008 Shredding XML

Hi I have some XML like this... 嗨我有这样的XML ...

<Questions>
  <Question1>A</Question1>
  <Question2>B</Question2>
  <Question3>C</Question3>
</Questions>

I would like to return A,B,C. 我想回A,B,C。 I have seen some similar questions but those had the same repeating node name. 我见过一些类似的问题,但那些具有相同的重复节点名称。 Unfortunately at this stage I cant change it. 不幸的是,在这个阶段我无法改变它。 I have played with the SQL Xpath syntax for AGES, with no luck. 我玩过AGES的SQL Xpath语法,没有运气。 I can get the whole Questions Node, but Ideally I would like the Actual Data. 我可以获得整个问题节点,但理想情况下我想要实际数据。 Multiple returned rows would also be OK. 多个返回的行也可以。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Update - Kirill's answer is very close, except that I have more than 1 record in the table and it is returning all records data within 1 row. 更新 - 基里尔的答案非常接近,除了我在表中有超过1条记录并且它返回1行内的所有记录数据。 If I could achieve a row/xml file that would be perfect !!! 如果我能实现一个完美的row / xml文件! Either by outputing another field from that record eg rownum or outputting another piece of data from the file eg ProfileName.. 通过从该记录输出另一个字段,例如rownum或从文件输出另一个数据,例如ProfileName ..

thanks, 谢谢,

Adrian ! 阿德里安!

declare @x xml = N'<Questions>
  <Question1>A</Question1>
  <Question2>B</Question2>
  <Question3>C</Question3>
</Questions>
'

SELECT x.value('.','varchar(10)')
FROM @x.nodes('/Questions/*') x(x)

Output 产量

----------
A
B
C

Continuing on Martin's solution and with some inspiration from this : 继续对马丁的解决方案,并与一些灵感

declare @x xml = N'<Questions>
  <Question1>A</Question1>
  <Question2>B</Question2>
  <Question3>C</Question3>
</Questions>
'

DECLARE @Questions VARCHAR(MAX)

SELECT @Questions = COALESCE(@Questions + ', ', '') + Question
FROM (
    SELECT x.value('.','varchar(10)') as Question
    FROM @x.nodes('/Questions/*') x(x)
) as y

SELECT @Questions

Output: 输出:

A, B, C

Use: 采用:

declare @x xml ='
<Questions>
  <Question1>A</Question1>
  <Question2>B</Question2>
  <Question3>C</Question3>
</Questions>'

select @x.value('(/*/Question1)[1]', 'nvarchar(max)')
    , @x.value('(/*/Question2)[1]', 'nvarchar(max)')
    , @x.value('(/*/Question3)[1]', 'nvarchar(max)')

Output: 输出:

---- ---- ----
A    B    C
select stuff(
(
    select ',' + x.value('.', 'varchar(10)') [text()]
    from @x.nodes('/*/*') x(x)
    for xml path(''))
, 1, 1, '')

Output: 输出:

A,B,C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM