Solved

Query syntax

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…

786 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