SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


query to return values from XML field


query to return values from XML field

Author
Message
Jampandu
Jampandu
SSC Veteran
SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)

Group: General Forum Members
Points: 260 Visits: 138
Hi ,

Can you help me in query to return values from XML field

create table #temp
(
id int identity (1,1)
,FieldSet XML
)


INSERT INTO #temp
VALUES ('<Fields> <Field Key="Column1" value="value1" > </Field> <Field Key="Column2" value="value2"> </Field> </Fields>')
,('<Fields> <Field Key="Column3" value="value3" > </Field> <Field Key="Column4" value="value4"> </Field> </Fields>')

SELECT * FROM #temp



Expected Output :

Id Column1 Column2 Column3 Column4
1 value1 value2 null null
2 null null value3 value4
Matt Crowley
Matt Crowley
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1101 Visits: 469
select FieldSet.value ('(/Fields/Field[@Key="Column1"]/@value)[1]', 'varchar(20)'),
FieldSet.value ('(/Fields/Field[@Key="Column2"]/@value)[1]', 'varchar(20)'),
FieldSet.value ('(/Fields/Field[@Key="Column3"]/@value)[1]', 'varchar(20)'),
FieldSet.value ('(/Fields/Field[@Key="Column4"]/@value)[1]', 'varchar(20)')
from #temp


Jampandu
Jampandu
SSC Veteran
SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)

Group: General Forum Members
Points: 260 Visits: 138
Thanks Matt!

Is there any way to select columns names dynamically as we may add more key values pairs in XML in near future.

just like 4th row

INSERT INTO #temp
VALUES ('<Fields> <Field Key="Column1" value="value1" > </Field> <Field Key="Column2" value="value2"> </Field> </Fields>')
,('<Fields> <Field Key="Column3" value="value3" > </Field> <Field Key="Column4" value="value4"> </Field> </Fields>')
,('<Fields> <Field Key="Column1" value="value1" > </Field> <Field Key="Column2" value="value2"> </Field><Field Key="Column3" value="value3" > </Field> <Field Key="Column4" value="value4"> </Field> </Fields>')
,('<Fields> <Field Key="Column1" value="value1" > </Field> <Field Key="Column2" value="value2"> </Field><Field Key="Column3" value="value3" > </Field> <Field Key="Column4" value="value4"> </Field> <Field Key="Column5" value="value5" > </Field> <Field Key="Column6" value="value6"> </Field><Field Key="Column7" value="value7" > </Field> <Field Key="Column8" value="value8"> </Field> </Fields>')



I don't have control on column names.


Thanks!
Matt Miller (4)
Matt Miller (4)
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29377 Visits: 19002
You probably want to split this into two different exercises. First extract the column name and values, and then use a dynamic pivot table to rebuild your rows.

The Dynamic pivot technique is described here.

In order to pull the data you want to pivot, you're looking at a somewhat different XML extraction:


create table #temp
(
id int identity (1,1)
,FieldSet XML
)

INSERT INTO #temp
VALUES ('<Fields> <Field Key="Column1" value="value1" > </Field> <Field Key="Column2" value="value2"> </Field> </Fields>')
,('<Fields> <Field Key="Column3" value="value3" > </Field> <Field Key="Column4" value="value4"> </Field> </Fields>')
,('<Fields> <Field Key="Column1" value="value1" ></Field>
<Field Key="Column2" value="value2"> </Field>
<Field Key="Column3" value="value3" > </Field>
<Field Key="Column4" value="value4"> </Field>
</Fields>')
,('<Fields> <Field Key="Column1" value="value1" > </Field>
<Field Key="Column2" value="value2"> </Field>
<Field Key="Column3" value="value3" > </Field>
<Field Key="Column4" value="value4"> </Field>
<Field Key="Column5" value="value5" > </Field>
<Field Key="Column6" value="value6"> </Field>
<Field Key="Column7" value="value7" > </Field>
<Field Key="Column8" value="value8"> </Field>
</Fields>')

--actual query starts here

select ID, F.value ('(@Key)[1]', 'varchar(20)') ColumnName, F.value ('(@value)[1]', 'varchar(20)') ColumnValue
into #temp2
from #temp cross apply fieldset.nodes('/Fields/Field') g(F)



----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Jampandu
Jampandu
SSC Veteran
SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)SSC Veteran (260 reputation)

Group: General Forum Members
Points: 260 Visits: 138
Thanks Miller,

Query working as expected , here is the query I used for dynamic pivot from #temp2 result



DECLARE @sql NVARCHAR(MAX)
DECLARE @dynamic_pivot NVARCHAR(MAX)

SET @dynamic_pivot = STUFF(( SELECT DISTINCT ',' + ColumnName FROM #temp2 FOR XML RAW, ELEMENTS, TYPE ).value('.', 'NVARCHAR(MAX)'), 1, 1, '' )

SET @sql = 'SELECT *
FROM #temp2 us
PIVOT ( MAX( ColumnValue ) FOR ColumnName In ( ' + @dynamic_pivot + ' ) ) AS pvt'

EXEC(@sql)


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search