1

I’m writing a query that returns data in XML format and I need to add a header that contains some static data, and a footer that contains a count of all of the records in the file. is there a way to do that so that the header and footer only appear once in the file?

my example query is as follows:

SET QUOTED_IDENTIFIER on
SET NOCOUNT ON
DECLARE @XMLTEST XML

SELECT @XMLTEST = (

SELECT GOLFER_NAME AS [GENERAL/NAME],
       GOLFER_ID AS [GENERAL/ID],
       HANDICAP_INDEX AS [ATTRIBUTES/HANDICAP],
       GOLFER_AGE AS [ATTRIBUTES/AGE]
FROM GOLFERS

FOR XML PATH ('row'), Root('root')
)

SET @XMLTEST.modify(
       'insert
        (
         attribute xsi:noNamespaceSchemaLocation {"ARIS_Inbound-v4.0.xsd"}
)
into (/GolfData)[1]')

SELECT @XMLTEST

This returns data in the following format:

<root>
 <row>
  <general>
   <name>woods,tiger</name>
   <id>1</id>
  </general>
  <attributes>
   <handicap>4</handicap>
   <age>45</age>
   </attributes>
  </row>
  <row>
   <general>
    <name>fowler,ricky</name>
    <id>2</id>
   </general>
   <attributes>
    <handicap>7</handicap>
    <age>39</age>
   </attributes>
  </row>
</root>

Which is what i am looking for but I need to have a header in the file with a format of:

<header>
<version>1.2.113</version>
<author>lincoln,abraham</author>
</header>

and a footer that counts the number of entries (or rows in this case) like this:

<trailer>
 <rowcount>2</rowcount>
</trailer>

is this possible?

1
  • No, this is not possible. Process the XML yourself after the query has executed using a DOM parser and add the new elements however you want.
    – Ken White
    Commented Nov 4, 2022 at 23:23

1 Answer 1

1

perhaps I'm overlooking the obvious, but couldn't you concatenate your normal results with your header and footer? Something like:

declare @header varchar(max) = "your header" 
declare @footerstart varchar(max) = "your footer start"
declare @footercount int;
declare @footerend varchar(max) = "your footer end"
declare @xmlMiddle varchar(max)

select @xmlMiddle = -- your code that generates the full XML.

--add a select count(*) from your dataset like
select @footercount = from.... where...

select @header + @xmlMiddle + @footerstart + @footercount + @footerend
4
  • No because that would put the header and trailer around each row which I don’t want. I just want one instance for the whole file
    – munale
    Commented Nov 4, 2022 at 23:10
  • Edited for greater clarity. At some point, you set your result to a variable.. the full result you need to add your header and footer to. Then, once you have your full XML, concatenate with the header and footer you want as I suggest? Really, in the end... XML is just a string, right?
    – Kevon
    Commented Nov 4, 2022 at 23:20
  • i get an error as follows : "Operand data type xml is invalid for add operator."
    – munale
    Commented Nov 7, 2022 at 15:23
  • Yes, your XML either needs to be converted to a string, or the other strings created as xml. I assumed you knew that part. See this other answer for more details on how to concatenate XML in the case you don't want to turn your XML into a string: stackoverflow.com/questions/11699914/…
    – Kevon
    Commented Nov 7, 2022 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.