Solved

Query syntax

Posted on 2014-07-30
2
160 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 500 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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