Solved

SQL XML For XML

Posted on 2011-02-16
5
329 Views
Last Modified: 2012-08-14
I need help creating a for xml path that looks like below...I have an outer node and then an inner node...See below for my desired output and regular SQL statement..<promotionInformation>
 is the outermost node and then <expandedPromotionsInformation> is the inner node..

<promotionInformation>
	  <expandedPromotionsInformation>		<promotionType>UserViewed_Value</promotionType>	<listingAddress>AddressLine1</listingAddress>		<promotionStartDate>DateStartPromotion</promotionStartDate>		<promotionEndDate>DateEndPromotion</promotionEndDate>
	</expandedPromotionsInformation>
 <summaryPromotionsInformation>
		<promotionType> UserViewed_Value </promotionType>
		<promotionEndDate> DateEndPromotion </promotionEndDate>
 </summaryPromotionsInformation>
</promotionInformation>



Select UserViewed_Value, AddressLine1 DateStartPromotion, DateEndPromotion
from Promotion

Open in new window

0
Comment
Question by:cheryl9063
  • 3
  • 2
5 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34909794
Where does "summaryPromotionsInformation" fit?
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34909814
If I read correctly, then

select
	UserViewed_Value 'expandedPromotionsInformation/promotionType',
	AddressLine1 'expandedPromotionsInformation/listingAddress',
	DateStartPromotion 'expandedPromotionsInformation/promotionStartDate',
	DateEndPromotion 'expandedPromotionsInformation/promotionEndDate',
	UserViewed_Value 'summaryPromotionsInformation/promotionType',
	DateEndPromotion 'summaryPromotionsInformation/promotionEndDate'
from Promotion
for xml path(''), root ('promotionInformation')

Open in new window

0
 
LVL 1

Author Comment

by:cheryl9063
ID: 34910520
I attached the real code below..

The result looks like this
<promotionInformation>
<expandedPromotionsInformation>
<UserViewed_Value>Open House</UserViewed_Value>
<promotionType>OpenHouse</promotionType>
<listingAddress>425 KENMORE Avenue</listingAddress>
</expandedPromotionsInformation>
</promotionInformation>

But I want this

<promotionInformation>
  <expandedPromotionsInformation>
    <promotionType>OpenHouse</promotionType>
    <listingAddress>425 KENMORE Avenue</listingAddress>
    <PromotionStartDate>null</PromotionStartDate>
    <PromotionEndDate>null</PromotionEndDate>
  </expandedPromotionsInformation>
 <SummaryPromotionsInformation>
      <promotionType>OpenHouse</promotionType>
       <PromotionEndDate>null</PromotionEndDate>
 </SummaryPromotionsInformation>
 </promotionInformation>       
Select pt.UserViewed_Value'expandedPromotionsInformation/UserViewed_Value',
pt.PromotionType'expandedPromotionsInformation/promotionType',
l.ListingAddressLine1'expandedPromotionsInformation/listingAddress',
DATENAME(w,lp.DateStartPromotion)+','+ DATENAME(month,lp.DateStartPromotion)+ ' ' + convert(varchar(25),DATEPART(MONTH,lp.DateStartPromotion))'expandedPromotionsInformation/promotionStartDate',
DATENAME(w,lp.DateEndPromotion)+','+ DATENAME(month,lp.DateEndPromotion)+ ' ' + convert(varchar(25),DATEPART(MONTH,lp.DateEndPromotion))'expandedPromotionsInformation/promotionEndDate'
from Listing l
join promotiontype pt on l.ListingID = pt.PromotionTypeID
join ListingPromotion lp on l.ListingID = lp.ListingPromotionID
 where l.listingID = 100000
for xml path(''), root ('promotionInformation')

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34912875
First of all, in XML

    <PromotionStartDate>null</PromotionStartDate>
    <PromotionEndDate>null</PromotionEndDate>

That is not valid.  Those would be interpreted downstream as the STRING value NULL.  The database value null is equivalent to the XML empty node, which FOR XML strips out.

Secondly, it would save us both some time to post the original query in the first place.

Lastly, the only trick to making UserViewedValue in two different places is to use it twice in the SELECT clause, with a different path for each , as I have shown in my example.
0
 
LVL 1

Author Closing Comment

by:cheryl9063
ID: 34964414
Thanks!!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now