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


Creating XML from select statement


Creating XML from select statement

Author
Message
bhushan_sahni
bhushan_sahni
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 13
I am trying to create an XML from select statement. I have created a Temp table from my query and now I am trying to create the XML. Here is what I have done till now.

select top 1 1 as Tag,
null as parent,
'' as [Invoice!1!Invoice],
null as [InvoiceDocumentDetails!2!InvoiceDocumentDate],
null as [InvoiceDocumentDetails!2!InvoiceDocumentNumber],
null as [InvoiceDocumentDetails!2!DocumentStatus]
from #tempTable

union all

select 2 as tag,
1 as parent,
'' as Invoice,
InvoiceDocumentDate ,
InvoiceDocumentNumber,
''
from #tempTable

Now how do I add next tag i.e. TradeAgreementReference and its attributes.

Here is a part of XML which I need to create
<Invoice>

<InvoiceDocumentDetails>
<InvoiceDocumentDate format="YYYY-MM-DDThh:mm:ss:TZD">2001-12-17T09:30:47</InvoiceDocumentDate>
<InvoiceDocumentNumber scheme="OTHER">Test invoice</InvoiceDocumentNumber>
<DocumentStatus codeList="EANCOM">9</DocumentStatus>
</InvoiceDocumentDetails>
<TradeAgreementReference>
<ContractReferenceDate format="YYYY-MM-DDThh:mm:ss:TZD">2001-12-17T09:30:47</ContractReferenceDate>
<ContractReferenceNumber scheme="OTHER">CG34</ContractReferenceNumber>
</TradeAgreementReference>
<OrderReference>
<PurchaseOrderDate format="YYYY-MM-DDThh:mm:ss:TZD">2001-12-17T09:30:47</PurchaseOrderDate>
<PurchaseOrderNumber scheme="OTHER">FR453287</PurchaseOrderNumber>
</OrderReference>
<OrderConfirmationReference>
<PurchaseOrderConfirmationDate format="YYYY-MM-DDThh:mm:ss:TZD">2001-12-17T09:30:47</PurchaseOrderConfirmationDate>
<PurchaseOrderConfirmationNumber scheme="OTHER">785545</PurchaseOrderConfirmationNumber>
</OrderConfirmationReference>
<DespatchReference>
<DespatchDocumentDate format="YYYY-MM-DDThh:mm:ss:TZD">2001-12-17T09:30:47</DespatchDocumentDate>
<DespatchDocumentNumber scheme="OTHER">687334</DespatchDocumentNumber>
</DespatchReference>
<ReceiptAdviceReference>
<ReceiptAdviceDocumentDate format="YYYY-MM-DDThh:mm:ss:TZD">2001-12-17T09:30:47</ReceiptAdviceDocumentDate>
<ReceiptAdviceDocumentNumber scheme="OTHER">GUY656</ReceiptAdviceDocumentNumber>
</ReceiptAdviceReference>
<Buyer>
<BuyerGLN scheme="GLN">5013546085276</BuyerGLN>
<BuyerAssigned scheme="OTHER">Compass</BuyerAssigned>
<SellerAssigned scheme="OTHER">A Supplier</SellerAssigned>
<Address>
<BuildingIdentifier scheme="OTHER">10</BuildingIdentifier>
<StreetName scheme="OTHER">Maltravers Street</StreetName>
<City scheme="OTHER">London</City>
<PostCode scheme="OTHER">WC2R 3BX</PostCode>
<Country codeList="ISO">GB</Country>
</Address>
</Buyer>
<\Invoice>


Please help me out. I hope I am clear, if not please let me know. Thanks in advance
wolfkillj
wolfkillj
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3974 Visits: 2582
Sounds like you should look into FOR XML:

http://msdn.microsoft.com/en-us/library/ms191268.aspx

Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
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