Solved

Query syntax

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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