Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Query syntax

Posted on 2014-07-30
2
Medium Priority
?
163 Views
Last Modified: 2014-07-31
Hello,

I have a table (see below) that contains a series of name/value pairs for a document.   The document is identified by the DocumentId column.  The name and values are given by the FieldName and FieldValue columns.  There is also a column called Changed (which is true when the FieldValue has been changed).

I need to produce an XML document like that shown below the table.   The elements are the name/value pairs.  Note that Changed must be an attribute of the element.

Do i need to pivot the data first, and then generate the xml?

How should the query look like?


Id	DocumentId	FieldName	FieldValue	                       Changed
161	9	                 text1		 Another form.	                        0
163	9	                 text2		 Soon it is time for lunch.!.	       1
165	9	                 colour		 3	                                          0

Open in new window


<Dataset>
<DocumentId>9</DocumentId>
<text1 changed=”0”>Another form</text1>
<text2 changed=”1”> Soon it is time for lunch.!</text2>
<colour changed=”0”>3</color>
<Dataset>

Open in new window

0
Comment
Question by:soozh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40229007
here is the way I would likely do... surely other ways to shave the cat
using a table variable for testing:
declare @t table ( Id int ,	DocumentId	int , FieldName	varchar(10), FieldValue	                     varchar(100),  Changed bit )
insert into @t  values (161	,9	                 ,'text1','Another form.',	                        0)
insert into @t  values (163	,9	                 ,'text2','Soon it is time for lunch.!.',	       1)
insert into @t  values (165	,9	                 ,'colour','3',                                         0)


select '<Dataset><DocumentId>' + cast(DocumentID as varchar(100)) + '</DocumentId>'
	+ replace(replace(( select '<' + x.FieldName + ' changed="' + cast(x.changed as varchar(10)) + '">' + x.FieldValue +  '</' + x.FieldName + '>' 
	      from @t x
	     where x.DocumentId = t.DocumentId
		   for xml path('')
	   ) ,'&lt;', '<'), '&gt;', '>')
	+'</Dataset>'
from @t t
group by DocumentID

Open in new window

0
 

Author Closing Comment

by:soozh
ID: 40231122
Thanks for the help.  I had to add some isnull statements because some of the columns had null values...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question