Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Mar 16

Written by: Steve Gray
3/16/2011 2:30 PM  RssIcon

In this example I need to return the contents of a table in one XML field.

We’re looking at the item table(s) and we need the guid, quantity, and cost. Every item needs to be returned. The result desired will look like this:

<root>
  <item guid="6A639774-5EA9-440C-978B-FDC7DBF71949">
    <quantityOnHand>0.00000</quantityOnHand>
    <cost>0.00000</cost>
  </item>
  <item guid="D8FD406F-8CF2-47E6-B09D-E44106153C11">
    <quantityOnHand>0.00000</quantityOnHand>
    <cost>0.00000</cost>
  </item>
</root>

This piece of code will do the trick

The query is wrapped in an <item> tag by the ‘for xml path(‘item’)’ clause, adding ROOT(‘ROOT’) will wrap the whole thing in a <root> tag.

The query returns the one column with an odd system generated name. Assigning the whole thing to the @items variable allows me to return a one row, one column dataset to the user with a predictable column name.

 

Code Snippet
  1. declare @items xml;
  2. set @items = (
  3.                 select top 2
  4.                         ix.itemguid as '@guid',
  5.                         iq.qtyonhnd as 'quantityOnHand',
  6.                         i.CURRCOST as 'cost'
  7.                     from iv00101 i
  8.                         join IV00101EXT ix with (nolock) on ix.ITEMNMBR = i.itemnmbr
  9.                         join IV00102 iq with (nolock) on iq.ITEMNMBR = i.ITEMNMBR
  10.                     where iq.LOCNCODE = 'psi'
  11.                     for xml path('item') , root('root')    
  12.                 )
  13.  
  14. select @items as items

Tags:
Categories:
Location: Blogs Parent Separator DEVSHED Blogs Child Separator SQL
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us