SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML Structure


XML Structure

Author
Message
dwomack
dwomack
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 3
I am trying to create xml from SQL that looks like what’s below.I am using Sql Server 2008 R2. Everything is working Correctly but the Bolded tags. I can’t seem to figure out how to get those in there. I am using FOR XML AUTO.


<Records>
<NewLoan>
<AppraisalValue>350000</AppraisalValue>
<AppraisalDate>4/18/2006</AppraisalDate>
<AdditionalBorrowers>
<AdditionalBorrower>
<BorrowerNumber>1</BorrowerNumber>
<AddBorrowerFirstName>Mickey</AddBorrowerFirstName>
<AddBorrowerLastName>Mouse</AddBorrowerLastName>
<AddBorrowerMiddleInitial>M</AddBorrowerMiddleInitial>
<AddBorrowerSSN>123456789</AddBorrowerSSN>
<AddBorrowerStreet>123 Main Street</AddBorrowerStreet>
<AddBorrowerCity>City Name</AddBorrowerCity>
<AddBorrowerState>NJ</AddBorrowerState>
<AddBorrowerZIP>08618</AddBorrowerZIP>
<AddBorrowerForeignAddress>N</AddBorrowerForeignAddress>
<AddBorrowerTelephone>1234567890</AddBorrowerTelephone>
</AdditionalBorrower>
<AdditionalBorrower>
<BorrowerNumber>2</BorrowerNumber>
<AddBorrowerFirstName>Minnie</AddBorrowerFirstName>
<AddBorrowerLastName>Mouse</AddBorrowerLastName>
<AddBorrowerMiddleInitial>M</AddBorrowerMiddleInitial>
<AddBorrowerSSN>123456780</AddBorrowerSSN>
<AddBorrowerStreet>125 Main Street</AddBorrowerStreet>
<AddBorrowerCity>City Name, England</AddBorrowerCity>
<AddBorrowerState></AddBorrowerState>
<AddBorrowerZIP>D5SF2</AddBorrowerZIP>
<AddBorrowerForeignAddress>Y</AddBorrowerForeignAddress>
<AddBorrowerTelephone>1234567891</AddBorrowerTelephone>
</AdditionalBorrower>
</AdditionalBorrowers>
<FloodCoCode>FRA5A</FloodCoCode>
<FloodProgram>R</FloodProgram>
</NewLoan>
</Records>


My Query Looks like…
Select Distinct
NewLoan.AppraisalValue, NewLoan.AppraisalDate,
AdditionalBorrower.BorrowerNumber, AdditionalBorrower.AddBorrowerFirstName, AdditionalBorrower.AddBorrowerMiddleName,AdditionalBorrower.AddBorrowerLastName, AdditionalBorrower.AddBorrowerSSN,
AdditionalBorrower.AddBorrowerStreet, AdditionalBorrower.AddBorrowerCity, AdditionalBorrower.AddBorrowerState, AdditionalBorrower.AddBorrowerZip,
AdditionalBorrower.AddBorrowerTelephone
FROM NewLoan LEFT OUTER JOIN
AdditionalBorrower ON NewLoan.loanGeneral_Id = AdditionalBorrower.loanGeneral_Id


for xml auto, ROOT ('RECORDS'), Elements ;

Thank you for any help
Ray M
Ray M
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2245 Visits: 1076
Pretty sure you cannot do this using XML Auto.
Try Using XML Path

Here's an example using Path from AdventureWorks
SELECT 
h.SalesOrderID,
h.OrderDate,
h.DueDate,
h.ShipDate,
h.Status,
h.SubTotal,
h.TaxAmt,
h.Freight,
h.TotalDue,
(SELECT
CarrierTrackingNumber,
OrderQty,
ProductID,
UnitPrice,
UnitPriceDiscount,
LineTotal,
ModifiedDate
FROM [Sales].[SalesOrderDetail] d
WHERE h.SalesOrderID = d.SalesOrderID
FOR XML PATH('OrderDetail'), ROOT('OrderDetails'), Type)
FROM [Sales].[SalesOrderHeader] h
WHERE h.SalesOrderID = 43666
FOR XML PATH('SalesOrder'), ROOT('SalesOrders')



<SalesOrders>
<SalesOrder>
<SalesOrderID>43666</SalesOrderID>
<OrderDate>2001-07-01T00:00:00</OrderDate>
<DueDate>2001-07-13T00:00:00</DueDate>
<ShipDate>2001-07-08T00:00:00</ShipDate>
<Status>5</Status>
<SubTotal>6079.6842</SubTotal>
<TaxAmt>486.3747</TaxAmt>
<Freight>151.9921</Freight>
<TotalDue>6718.0510</TotalDue>
<OrderDetails>
<OrderDetail>
<CarrierTrackingNumber>D46A-40CA-8D</CarrierTrackingNumber>
<OrderQty>1</OrderQty>
<ProductID>764</ProductID>
<UnitPrice>419.4589</UnitPrice>
<UnitPriceDiscount>0.0000</UnitPriceDiscount>
<LineTotal>419.458900</LineTotal>
<ModifiedDate>2001-07-01T00:00:00</ModifiedDate>
</OrderDetail>
<OrderDetail>
<CarrierTrackingNumber>D46A-40CA-8D</CarrierTrackingNumber>
<OrderQty>1</OrderQty>
<ProductID>753</ProductID>
<UnitPrice>2146.9620</UnitPrice>
<UnitPriceDiscount>0.0000</UnitPriceDiscount>
<LineTotal>2146.962000</LineTotal>
<ModifiedDate>2001-07-01T00:00:00</ModifiedDate>
</OrderDetail>
</OrderDetails>
</SalesOrder>
</SalesOrders>

dwomack
dwomack
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 3
Perfect! Thank you so much
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search