Header in SQL XML PATH

I have the following select statement for an XML ouput:
select 
--header
DocNum AS 'DocNum',
CONVERT(VARCHAR(8), ODLN.DocDate, 112) + LEFT(REPLACE(CONVERT(varchar, ODLN.DocDate, 108), ':',''),4) as 'DocDate', 
CONVERT(VARCHAR(8), ODLN.DocDueDate, 112) + LEFT(REPLACE(CONVERT(varchar, ODLN.DocDueDate, 108), ':',''),4) as 'DocDueDate', 
ODLN.CardCode as 'CardCode',
NumAtCard as 'NumAtCard',
BILLTO.GlblLocNum as 'BILLTO/GLN',
BILLTO.Address as 'BILLTO/Address',
BILLTO.Street as 'BILLTO/Street',
BILLTO.ZipCode as 'BILLTO/ZipCode',
BILLTO.City as 'BILLTO/City',
BILLTO.Country as 'BILLTO/Country',
BILLTO.LicTradNum as 'BILLTO/VAT',
SHIPTO.Address as 'SHIPTO/Address',
SHIPTO.GlblLocNum as 'SHIPTO/GLN', 
SHIPTO.Street as 'SHIPTO/Street',
SHIPTO.ZipCode as 'SHIPTO/ZipCode',
SHIPTO.City as 'SHIPTO/City',
SHIPTO.Country as 'SHIPTO/Country',
SHIPTO.LicTradNum as 'SHIPTO/VAT',

--data
DLN1.ItemCode as 'ITEMS/ItemCode', 
DLN1.Dscription as 'ITEMS/Description', 
Quantity  as 'ITEMS/Quantity' 

from ODLN 
inner join DLN1 on ODLN.DocEntry=DLN1.DocEntry 
inner join OCRD on OCRD.CardCode=ODLN.CardCode

JOIN
-- Facturatieadres
(select GlblLocNum, Address, AdresType, Street, ZipCode, City, Country, LicTradNum from CRD1) BILLTO
 on BILLTO.Address=ODLN.CardName 

JOIN
-- leveringsadres
(select GlblLocNum, Address, AdresType, Street, ZipCode, City, Country, LicTradNum from CRD1) SHIPTO
 on SHIPTO.Address=ODLN.ShipToCode
where ODLN.DocEntry = '8768' 


for XML PATH ('Documents') ;

Open in new window


I want the header info to appear only once in the ouput.

Any idea how ?
kimtheysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Without a concise and complete example..

What does "only once" mean? Post a sample of the desired output.

You normally need a sub-query for the descendants in your output XML. E.g.

SELECT  S.name AS [schema_name] ,
        T.name AS [table_name]
FROM    sys.schemas S
        INNER JOIN sys.tables T ON T.schema_id = S.schema_id
FOR     XML PATH('') ,
            ROOT('database');

SELECT  S.name AS [schema_name] ,
        T.table_names AS [table_names]
FROM    sys.schemas S
        CROSS APPLY ( SELECT    T.name
                      FROM      sys.tables T
                      WHERE     T.schema_id = S.schema_id
                    FOR
                      XML PATH('') ,
                          TYPE
                    ) T ( table_names )
FOR     XML PATH('') ,
            ROOT('database');

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kimtheysAuthor Commented:
I mean 'only once' because my header info repeats:

Now I get:<Documents>
      <DocNum>157208768</DocNum>
      <DocDate>201509090000</DocDate>
      <DocDueDate>201509090000</DocDueDate>
      <CardCode>C344</CardCode>
      <NumAtCard>905525000624</NumAtCard>
      <BILLTO>
            <GLN>5400150000007</GLN>
            <Address>MAKRO CASH &amp; CARRY BELGIUM NV</Address>
            <Street>NIJVERHEIDSSTRAAT 70</Street>
            <ZipCode>2160</ZipCode>
            <City>WOMMELGEM</City>
            <Country>BE</Country>
            <VAT>BE0406952018</VAT>
      </BILLTO>
      <SHIPTO>
            <Address>MAKRO SPL</Address>
            <GLN>5400150500002</GLN>
            <Street>STEENWEG OP BERGEN 427</Street>
            <ZipCode>1600</ZipCode>
            <City>ST.-PIETERS-LEEUW</City>
            <Country>BE</Country>
            <VAT>BE0406952018</VAT>
      </SHIPTO>
      <ITEMS>
            <LineNum>0</LineNum>
            <ItemCode>VPN5810</ItemCode>
            <CodeBars>03600905003570</CodeBars>
            <Description>QUINOA BUILTJES 2X120G</Description>
            <Quantity>2.000000</Quantity>
            <UoM>CRT</UoM>
            <OrderedQTY>2.000000</OrderedQTY>
            <InvQTY>24.000000</InvQTY>
            <OpenInvQty>0.000000</OpenInvQty>
      </ITEMS>
</Documents>
<Documents>
      <DocNum>157208768</DocNum>
      <DocDate>201509090000</DocDate>
      <DocDueDate>201509090000</DocDueDate>
      <CardCode>C344</CardCode>
      <NumAtCard>905525000624</NumAtCard>
      <BILLTO>
            <GLN>5400150000007</GLN>
            <Address>MAKRO CASH &amp; CARRY BELGIUM NV</Address>
            <Street>NIJVERHEIDSSTRAAT 70</Street>
            <ZipCode>2160</ZipCode>
            <City>WOMMELGEM</City>
            <Country>BE</Country>
            <VAT>BE0406952018</VAT>
      </BILLTO>
      <SHIPTO>
            <Address>MAKRO SPL</Address>
            <GLN>5400150500002</GLN>
            <Street>STEENWEG OP BERGEN 427</Street>
            <ZipCode>1600</ZipCode>
            <City>ST.-PIETERS-LEEUW</City>
            <Country>BE</Country>
            <VAT>BE0406952018</VAT>
      </SHIPTO>
      <ITEMS>
            <LineNum>1</LineNum>
            <ItemCode>VPN1773</ItemCode>
            <CodeBars>03165440007358</CodeBars>
            <Description>COUSCOUS BUILTJES  5X100G</Description>
            <Quantity>48.000000</Quantity>
            <UoM>ST</UoM>
            <OrderedQTY>48.000000</OrderedQTY>
            <InvQTY>48.000000</InvQTY>
            <OpenInvQty>0.000000</OpenInvQty>
      </ITEMS>
</Documents>

I would like to see it like this:
<Documents>
<Header>
      <DocNum>157208768</DocNum>
      <DocDate>201509090000</DocDate>
      <DocDueDate>201509090000</DocDueDate>
      <CardCode>C344</CardCode>
      <NumAtCard>905525000624</NumAtCard>
      <BILLTO>
            <GLN>5400150000007</GLN>
            <Address>MAKRO CASH &amp; CARRY BELGIUM NV</Address>
            <Street>NIJVERHEIDSSTRAAT 70</Street>
            <ZipCode>2160</ZipCode>
            <City>WOMMELGEM</City>
            <Country>BE</Country>
            <VAT>BE0406952018</VAT>
      </BILLTO>
      <SHIPTO>
            <Address>MAKRO SPL</Address>
            <GLN>5400150500002</GLN>
            <Street>STEENWEG OP BERGEN 427</Street>
            <ZipCode>1600</ZipCode>
            <City>ST.-PIETERS-LEEUW</City>
            <Country>BE</Country>
            <VAT>BE0406952018</VAT>
      </SHIPTO>
</Header>
<ITEMS>
      <LineNum>0</LineNum>
      <ItemCode>VPN5810</ItemCode>
      <CodeBars>03600905003570</CodeBars>
      <Description>QUINOA BUILTJES 2X120G</Description>
      <Quantity>2.000000</Quantity>
      <UoM>CRT</UoM>
      <OrderedQTY>2.000000</OrderedQTY>
      <InvQTY>24.000000</InvQTY>
      <OpenInvQty>0.000000</OpenInvQty>
      <LineNum>1</LineNum>
      <ItemCode>VPN1773</ItemCode>
      <CodeBars>03165440007358</CodeBars>
      <Description>COUSCOUS BUILTJES  5X100G</Description>
      <Quantity>48.000000</Quantity>
      <UoM>ST</UoM>
      <OrderedQTY>48.000000</OrderedQTY>
      <InvQTY>48.000000</InvQTY>
      <OpenInvQty>0.000000</OpenInvQty>
</ITEMS>
</Documents>
0
ste5anSenior DeveloperCommented:
Take a look at my sample again.

It's first of all the difference between PATH() and ROOT(). Then you're using a JOIN. Which is not suited for this kind of output. You need a sub-query as seen in my sample.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.