Advertisement

04.02.2008 at 01:31AM PDT, ID: 23288419
[x]
Attachment Details

SQL Select Statement for XML Explicit formatting from a single table

Asked by StephenBudden in MS SQL Server, Extensible Markup Language (XML), Microsoft Development

Tags: Microsoft, SQL, 2000,2005, T-SQL, TSQL

I have a simple table that has data I need to export to an XML formatted file.  I can get the data formatted correctly when it is one record but when there is 2 or more the Select..for XML explicit statement seems to be incorrect.  I have tried many iterations with no success.  Here is the temporary table format followed by the Select statement and what I'm trying to get out of the statement.

TagName      TimeStamp            Value
ABCDEFG      2008-04-02 04:11:00      12345
WXYZ      2008-04-02 16:15:00      98765

This is the format that I need
<Import>
<DataList Version="1.0.71">
<Tag Name="ABCDEFG"/>
  <Data>
    <TimeStamp>2008-04-02 04:11:00</TimeStamp>
    <Value>12345</Value>
    <DataQuality>GOOD</DataQuality>
  </Data>
</Tag>
<Tag Name="WXYZ">
  <Data>
    <TimeStamp>2008-04-02 16:15:00</TimeStamp>
    <Value>98765</Value>
    <DataQuality>GOOD</DataQuality>
  </Data>
</Tag>
</DataList>
</Import>

This is the current SQL statement I'm using.

I can't seem to get the TagName to nest within the output.  I seem to show all the TagNames at the beginning of the statement and not within the XML format structure.

Any help would be greatly appreciated.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
SELECT 1   	as Tag,
       NULL	as Parent,
       NULL	as [Import!1],
       NULL	as [DataList!2!Version],
       NULL       as [Tag!3!Name],
       NULL	as [Data!4!TimeStamp!Element],
       NULL	as [Data!4!Value!Element],
       NULL	as [Data!4!DataQuality!Element]
from ##Output
union
SELECT 2, 
       1,
       NULL,
       '1.0.71',
       NULL,
       NULL,
       NULL,
       NULL 
FROM ##Output
union all
SELECT 3, 
       2,
       NULL,
       NULL,
       TagName,
       NULL,
       NULL,
       NULL
FROM ##Output
union all
SELECT 4, 
       3,
       NULL,
       NULL,
       TagName,
       CONVERT(varchar(20),Timestamp,120),
       Value,
       'GOOD'
FROM ##Output
ORDER BY 2,3,4 
FOR XML EXPLICIT
[+][-]04.03.2008 at 02:38AM PDT, ID: 21270807

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.04.2008 at 05:34AM PDT, ID: 21280950

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.04.2008 at 07:52AM PDT, ID: 21282347

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, Extensible Markup Language (XML), Microsoft Development
Tags: Microsoft, SQL, 2000,2005, T-SQL, TSQL
Sign Up Now!
Solution Provided By: mark_wills
Participating Experts: 1
Solution Grade: A
 
 
[+][-]04.09.2008 at 05:13PM PDT, ID: 21320407

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]04.19.2008 at 04:07AM PDT, ID: 21391914

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628