Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 for xml explicit: DESC ordering breaks it?

Author  Topic 

aiken
Aged Yak Warrior

525 Posts

Posted - 2005-11-10 : 14:20:57
This is driving me insane. I have a perfectly happy FOR XML EXPLICIT query that works beautifully... except it returns the results in wrong order. The project is a personals type site, and the intent here is to return an XML doc with all of the countries (and states, if applicable) in a region, ordered by countries with the most profiles first.

The whole thing works fine with the default (ASC) ordering for [country!2!personals_count!element], but putting in DESC breaks it with the error "Parent tag ID 1 is not among the open tags. FOR XML EXPLICIT requires parent tags to be opened first. Check the ordering of the result set."

The code:

select 1 as Tag,
NULL as Parent,
regions.i as [region!1!region_id],
regions.name as [region!1!region_name!element],
regions.personals_count as [region!1!personals_count!element],
NULL as [country!2!country_id],
NULL as [country!2!country_name!element],
NULL as [country!2!personals_count!element],
NULL as [state!3!state_id],
NULL as [state!3!state_name!element],
NULL as [state!3!personals_count!element]
from regions
where regions.[name]='Europe / UK'

union all

select 2 as Tag,
1 as Parent,
regions.i as [region!1!region_id],
regions.name as [region!1!region_name!element],
regions.personals_count as [region!1!personals_count!element],
countries.i as [country!2!country_id],
countries.name as [country!2!country_name!element],
countries.personals_count as [country!2!personals_count!element],
NULL as [state!3!state_id],
NULL as [state!3!state_name!element],
NULL as [state!3!personals_count!element]
from countries
join regions_countries on regions_countries.i_countries=countries.i
join regions on regions_countries.i_regions=regions.i
where regions.name='Europe / UK'

union all

select 3 as Tag,
2 as Parent,
regions.i as [region!1!region_id],
regions.name as [region!1!region_name!element],
regions.personals_count as [region!1!personals_count!element],
countries.i as [country!2!country_id],
countries.name as [country!2!country_name!element],
countries.personals_count as [country!2!personals_count!element],
states.i as [state!3!state_id],
states.[name] as [state!3!state_name!element],
states.personals_count as [state!3!personals_count!element]
from states
join countries_states on countries_states.i_states=states.i
join countries on countries_states.i_countries=countries.i
join regions_countries on regions_countries.i_countries=countries.i
join regions on regions_countries.i_regions=regions.i
where regions.name='Europe / UK'

order by [region!1!region_id],[country!2!personals_count!element],[state!3!state_id]

FOR XML EXPLICIT


That will work; changing the order by to read "order by [region!1!region_id],[country!2!personals_count!element] desc,[state!3!state_id]" will break it.

Heeeeeeelp!

Thanks
-b

aiken
Aged Yak Warrior

525 Posts

Posted - 2005-11-10 : 15:08:55
Well, I discovered that I really shouldn't have been doing it this way anyway; including an <xsl:sort> in the stylesheet seems like a much better idea.

However, I'm still curious about why this breaks. Anyone got any insight?

Cheers
-b
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-11-10 : 15:18:19
What happens if you prefix the ORDER BY with "[tag], [Parent]"?

I suspect that's pretty "key"

Having said that, we have a number of XML tasks that have the "top level" PK columns first, then TAG, then the second-level-PKs etc.

Kristen
Go to Top of Page

aiken
Aged Yak Warrior

525 Posts

Posted - 2005-11-10 : 15:56:27
quote:
Originally posted by Kristen
What happens if you prefix the ORDER BY with "[tag], [Parent]"?



Well, that at least keeps it from breaking, but it leaves the states associated with the wrong countries ("Mexico -> Alaska, Mexico -> Alabama", etc).

Thanks for the thought, but I think for now I'll just keep Alaska as part of the US and use sorting on the XML side.

Cheers
-b
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-11-10 : 22:23:57
Well we've got some with PK stuff ahead of the TAG. Maybe you just need to add TAG onto the end of your preferred list

ORDER BY Country, State, TAG ??

or maybe you could rejig your TAG numbers so that they reflect the sort order?

Kristen

Go to Top of Page
   

- Advertisement -