Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

XML Output - Fields in reverse order? Expand / Collapse
Author
Message
Posted Tuesday, March 05, 2013 4:03 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, March 31, 2014 9:19 AM
Points: 314, Visits: 677
Guys,

For some reason the XML output given by SSRS 2008 seems to put the fields in reverse order, has anyone come across this, it seems very wierd. For example, take this simple code:

SELECT
1 as ID,
'ABC Capital' as Firm_Name,
'123 The Lane' as Firm_Address_1,
'London' as Firm_City,
100000 as AUM_USD,
'Retail, Consumer Goods' as Industry

UNION

SELECT
2 as ID,
'AXyZ Capital' as Firm_Name,
'333 The Lane' as Firm_Address_1,
'New York' as Firm_City,
9999 as AUM_USD,
'Food' as Industry

UNION

SELECT
3 as ID,
'ABC Capital' as Firm_Name,
'1 The Lane' as Firm_Address_1,
'Milan' as Firm_City,
555 as AUM_USD,
'Agriculture' as Industry


The industry field comes first and the ID field last.

I suppose this doesn't matter but conceptually it just looks odd.

The output given by this:

SELECT * FROM
(
SELECT
1 as ID,
'ABC Capital' as Firm_Name,
'123 The Lane' as Firm_Address_1,
'London' as Firm_City,
100000 as AUM_USD,
'Retail, Consumer Goods' as Industry

UNION

SELECT
2 as ID,
'AXyZ Capital' as Firm_Name,
'333 The Lane' as Firm_Address_1,
'New York' as Firm_City,
9999 as AUM_USD,
'Food' as Industry

UNION

SELECT
3 as ID,
'ABC Capital' as Firm_Name,
'1 The Lane' as Firm_Address_1,
'Milan' as Firm_City,
555 as AUM_USD,
'Agriculture' as Industry
)x
FOR XML PATH

In Management Studio, seems to make much more sense- it's in the same order as the select (not to mention the formatting).

Has anyone else come across this, if so did they just put up with it, not use XML output from SSRS or do something different?

Thanks in advance!
Post #1426718
Posted Tuesday, March 05, 2013 11:36 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
This is what I got when I used your code in SSMS as is. It looks correct for me. So perhaps something specific to your database settings (like collation) is making it sort differently?

<row>
<ID>1</ID>
<Firm_Name>ABC Capital</Firm_Name>
<Firm_Address_1>123 The Lane</Firm_Address_1>
<Firm_City>London</Firm_City>
<AUM_USD>100000</AUM_USD>
<Industry>Retail, Consumer Goods</Industry>
</row>
<row>
<ID>2</ID>
<Firm_Name>AXyZ Capital</Firm_Name>
<Firm_Address_1>333 The Lane</Firm_Address_1>
<Firm_City>New York</Firm_City>
<AUM_USD>9999</AUM_USD>
<Industry>Food</Industry>
</row>
<row>
<ID>3</ID>
<Firm_Name>ABC Capital</Firm_Name>
<Firm_Address_1>1 The Lane</Firm_Address_1>
<Firm_City>Milan</Firm_City>
<AUM_USD>555</AUM_USD>
<Industry>Agriculture</Industry>
</row>

Post #1426960
Posted Tuesday, March 05, 2013 11:45 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
You can check the collation on your db with:
(code from Pinal Dave)


SELECT
c.name
,c.collation_name
FROM
sys.columns c
WHERE
OBJECT_ID IN
(
SELECT
OBJECT_ID
FROM
sys.objects o
WHERE
type = 'U'
AND o.name = 'YourTableName'
)


Just for reference, on my machine the collation is 'SQL_Latin1_General_CP1_CI_AS'.

 
Post #1426964
Posted Wednesday, March 06, 2013 1:54 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, March 31, 2014 9:19 AM
Points: 314, Visits: 677
I meant to reply yesterday afternoon, I stumbled on the solution, sort of by accident!

On the table in question 'DataElemetStyle' was just left at the default of auto, changing this to Element had two impacts:

1. The XML was far nicer to read - not just one massive block for each row
2. The fields were in the correct order

It does seem odd that in the default setting it looks hideous and the fields are inverted in their order, however, an element structure seems to cure this :)

Odd but there you go, I wonder if anyone else can replicate this or if it's just me!
Post #1427239
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse