Solved

SQL XML For XML

Posted on 2011-02-16
5
327 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Copy Database Wizard 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 video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

10 Experts available now in Live!

Get 1:1 Help Now