Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I needed to do the same but without knowing how many rows/variables were concerned and without extra schema added so here was my solution. Following this pattern, I can generate as many snippets as I want, combine them, pass them between PROCS or even return them from procs and at any point, wrap them up in containers all without modifying the data or being forced to add XML structure into my data. I use this approach with HTTP end points to provide XML Web services and with another trick that converts XML into JSON, to provide JSON WebServices.</p> <pre><code> -- SETUP A type (or use this design for a Table Variable) to temporarily store snippets into. The pattern can be repeated to pass/store snippets to build -- larger elements and those can be further combined following the pattern. CREATE TYPE [dbo].[XMLRes] AS TABLE( [xmlResult] [xml] NULL ) GO -- Call the following as much as you like to build up all the elements you want included in the larger element INSERT INTO @XMLRes ( xmlResult ) SELECT ( SELECT 'foo' '@bar' FOR XML PATH('SomeTopLevelElement') ) -- This is the key to "concatenating" many snippets into a larger element. At the end of this, add " ,ROOT('DocumentRoot') " to wrapp them up in another element even -- The outer select is a time from user2503764 that controls the output column name SELECT ( SELECT XmlData as [*] FROM ( SELECT xmlResult AS [*] FROM @XmlRes WHERE xmlResult IS NOT NULL FOR XML PATH(''), TYPE ) as DATA(XmlData) FOR XML PATH('') ) as [someColumnName] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload