PDA

View Full Version : [T-SQL][XQuery] xml a table


Darker
21-12-2010, 23:36
Ciao a tutti :)

Vorrei scrivere una funzione che, dato un xml in input formattato, mi permetta di generare automaticamente una tabella le cui colonne sono gli attributi dell'xml e le righe i valori.

Quello che voglio ottenere è una funzione automattizzata che faccia esattamente quello che fa questa, con la differenza che non voglio specificare i campi.

DECLARE @String varchar(max) =
'<root>
<entry>
<code>codeTest</code>
<site>siteTest</site>
<isEnabled>1</isEnabled>
<isRealMoney>1</isRealMoney>
</entry>
<entry>
<code>codeTest2</code>
<site>siteTest2</site>
<isEnabled>0</isEnabled>
<isRealMoney>1</isRealMoney>
</entry>
</root>'

DECLARE @xml XML = CONVERT(xml, @String)

SELECT
x.value('(code)[1]', 'varchar(32)') as Code,
x.value('(site)[1]', 'varchar(32)') as SiteCode,
x.value('(isEnabled)[1]', 'bit') as IsEnabled,
x.value('(isRealMoney)[1]', 'bit') as IsRealMoney

FROM @xml.nodes('//root/entry') x(x)

E' possibile ottenere qualcosa? :)