簡體   English   中英

Sql Server 2008粉碎XML

[英]Sql Server 2008 Shredding XML

嗨我有這樣的XML ...

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

我想回A,B,C。 我見過一些類似的問題,但那些具有相同的重復節點名稱。 不幸的是,在這個階段我無法改變它。 我玩過AGES的SQL Xpath語法,沒有運氣。 我可以獲得整個問題節點,但理想情況下我想要實際數據。 多個返回的行也可以。

任何幫助將不勝感激。

更新 - 基里爾的答案非常接近,除了我在表中有超過1條記錄並且它返回1行內的所有記錄數據。 如果我能實現一個完美的row / xml文件! 通過從該記錄輸出另一個字段,例如rownum或從文件輸出另一個數據,例如ProfileName ..

謝謝,

阿德里安!

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)

產量

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

繼續對馬丁的解決方案,並與一些靈感

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

輸出:

A, B, C

采用:

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)')

輸出:

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

輸出:

A,B,C

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM