|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jun 2001
Città: London (UK)
Messaggi: 1788
|
[T-SQL][XQuery] xml a table
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. Codice:
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)
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:15.



















