Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

FOR XML PATH question

 
   Database Help (Home) -> XML RSS
Next:  At my Wits end with this one  
Author Message
Sean

External


Since: Apr 03, 2008
Posts: 3



(Msg. 1) Posted: Wed Dec 31, 2008 5:49 am
Post subject: FOR XML PATH question
Archived from groups: microsoft>public>sqlserver>xml (more info?)

Hello all,

I am trying to get a list of materials needed for and item in an xml
format such as

<item ...attributes...>
<material ...attributes.../>
<material ...attributes.../>
</item>

I have the following query built, but it gives me (would there have
been 2 materials needed)
<item ..attributes...>
<material ...attributes.../>
</item>
<item (same item)..attributes...>
<material ...attributes.../>
</item>


SELECT item.typeID AS "@typeID",
ig.groupName AS "@groupName",
item.typeName AS "@typeName",
material.requiredTypeID AS "Material/@typeID",
(SELECT item.typeName
FROM invTypes item
WHERE item.typeID = material.requiredTypeID) AS "Material/
@typeName",
material.quantity AS "Material/@quantity"
FROM invTypes item INNER JOIN typeActivityMaterials material ON
item.typeID = material.typeID
INNER JOIN invGroups ig ON item.groupID = ig.groupID
WHERE item.published = 1
ORDER BY item.typeID
FOR XML PATH('Item'), ROOT('Items')

 >> Stay informed about: FOR XML PATH question 
Back to top
Login to vote
Bob

External


Since: Feb 08, 2005
Posts: 182



(Msg. 2) Posted: Sat Jan 03, 2009 9:26 am
Post subject: RE: FOR XML PATH question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Something like this?

SELECT item.typeID AS "@typeID",
ig.groupName AS "@groupName",
item.typeName AS "@typeName",
(
SELECT
material.requiredTypeID AS "Material/@typeID",
item2.typeName AS "Material/@typeName",
material.quantity AS "Material/@quantity"
FROM typeActivityMaterials material
INNER JOIN invTypes item2 ON item2.typeID = material.requiredTypeID
WHERE item.typeID = material.typeID FOR XML PATH(''), TYPE
)
FROM invTypes item
INNER JOIN invGroups ig ON item.groupID = ig.groupID
WHERE item.published = 1
ORDER BY item.typeID
FOR XML PATH('Item'), ROOT('Items')

It's hard to tell without sample data tho.

Let me know how you get on.

wBob


"Sean" wrote:

> Hello all,
>
> I am trying to get a list of materials needed for and item in an xml
> format such as
>
> <item ...attributes...>
> <material ...attributes.../>
> <material ...attributes.../>
> </item>
>
> I have the following query built, but it gives me (would there have
> been 2 materials needed)
> <item ..attributes...>
> <material ...attributes.../>
> </item>
> <item (same item)..attributes...>
> <material ...attributes.../>
> </item>
>
>
> SELECT item.typeID AS "@typeID",
> ig.groupName AS "@groupName",
> item.typeName AS "@typeName",
> material.requiredTypeID AS "Material/@typeID",
> (SELECT item.typeName
> FROM invTypes item
> WHERE item.typeID = material.requiredTypeID) AS "Material/
> @typeName",
> material.quantity AS "Material/@quantity"
> FROM invTypes item INNER JOIN typeActivityMaterials material ON
> item.typeID = material.typeID
> INNER JOIN invGroups ig ON item.groupID = ig.groupID
> WHERE item.published = 1
> ORDER BY item.typeID
> FOR XML PATH('Item'), ROOT('Items')
>

 >> Stay informed about: FOR XML PATH question 
Back to top
Login to vote
Sean

External


Since: Apr 03, 2008
Posts: 3



(Msg. 3) Posted: Fri Jan 09, 2009 6:17 am
Post subject: Re: FOR XML PATH question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Worked like a charm, I didn't realise that you had to make it a
complete subquery in order to get it to nest.

Thank you!
 >> Stay informed about: FOR XML PATH question 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
SQL 2005 For Xml Path question - In the SQL 2005 Books Online section titled "Using PATH Mode", there is an example near the bottom that results in the following XML. Is there a way to get the xml so it's encoded correctly (not using < >)? Not sure why it's doing th...

tuning question, relation between indexes and "for xml pat.. - ,Hi, I'm facing very strange problem here. I made index analysis by quering dynamic views, and found that bunch of indexes is not being used at all. Beside that, I found bunch of indexes which are duplicated. For example, on the same set of columns,..

How to add some attributes when I use For Xml Path? - How to add some attributes when I use For Xml Path? As I developed the sql statement using For Xml Path, the users now required add new attributes to some tags. For Example: SQL: Select TOP 2 Code, Name From City For Xml Path('City') <City> ...

Loading XML Schema from a file using a RELATIVE path - Good Evening All, I've seen examples of how to load an XML schema from a file; however, all of them use an absolute file path. I'd like to load it from a path relative to the location were the SQL script is. This will allow us to access the XSD file..

BulkLoad question - In order to upload data using SQLXML Bulkload, must the data and schema reside in a file, or can you store the data to a variable, then use Bulkload ?
   Database Help (Home) -> XML All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]