Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL FOR XML EXPLICIT

Not able to to get the desired XML output

The following:

  SELECT  1 as Tag,
          0 as Parent,
          sID       as [Document!1!sID], 
          docID     as [Document!1!docID],
          null      as [To!2!value]
  FROM docSVsys with (nolock)
  where docSVsys.sID = '57'
  UNION ALL 
  SELECT 2 as Tag,
         1 as Parent,
         sID,
         NULL,
         value         
  FROM   docMVtext
  WHERE  docMVtext.sID = '57'
  ORDER BY [Document!1!sID],[To!2!value]
  FOR XML EXPLICIT;

Produces:

    <Document sID="57" docID="3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA">
      <To value="Frank Ermis" />
      <To value="Keith Holst" />
      <To value="Mike Grigsby" />
    </Document>

What I want is:

    <Document sID="57">
      <docID>3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA</docID>
      <To>
        <Value>Frank Ermis</Value>
        <Value>Keith Holst</Value>
        <Value>Mike Grigsby</Value>
      </To>
    </Document>

Can I get that ouput with FOR XML?

Ok I get they may be technically equivalent.
What I want and what I need are not the same.

Using xDocument for this is is SLOW.
There are millions of documents and need to XML up to 1 million at a time to XML.
The TSQL FOR XML is super fast.
I just need to get FOR XML to format.

The solution (based on accepted answer):

   SELECT top 4
     [sv].[sID] AS '@sID'
    ,[sv].[sParID] AS '@sParID' 
    ,[sv].[docID] AS 'docID'
    ,[sv].addDate as 'addDate'
    ,(SELECT [value] AS 'value'
       FROM  [docMVtext] as [mv]
       WHERE [mv].[sID] = [sv].[sID]
         AND [mv].[fieldID] = '113'
       ORDER BY [mv].[value]
       FOR XML PATH (''), type
     ) AS "To"  
    ,(SELECT [value] AS 'value'
       FROM  [docMVtext] as [mv]
       WHERE [mv].[sID] = [sv].[sID]
         AND [mv].[fieldID] = '130'
       ORDER BY [mv].[value]
       FOR XML PATH (''), type
     ) AS "MVtest" 
  FROM  [docSVsys] as [sv]
  WHERE [sv].[sID] >= '57' 
  ORDER BY 
      [sv].[sParID], [sv].[sID]
  FOR XML PATH('Document'), root('Documents')

Produces:

<Documents>
  <Document sID="57" sParID="57">
    <docID>3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA</docID>
    <addDate>2011-10-28T12:26:00</addDate>
    <To>
      <value>Frank Ermis</value>
      <value>Keith Holst</value>
      <value>Mike Grigsby</value>
    </To>
    <MVtest>
      <value>MV test 01</value>
      <value>MV test 02</value>
      <value>MV test 03</value>
      <value>MV test 04</value>
    </MVtest>
  </Document>
  <Document sID="58" sParID="57">
    <docID>3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA.1</docID>
    <addDate>2011-10-28T12:26:00</addDate>
  </Document>
  <Document sID="59" sParID="59">
    <docID>3.818920.KJKP5LYKTNIODOEI4JDOKJ2BXJI5P0BIA</docID>
    <addDate>2011-10-28T12:26:00</addDate>
    <To>
      <value>Vladimir Gorny</value>
    </To>
  </Document>
  <Document sID="60" sParID="59">
    <docID>3.818920.KJKP5LYKTNIODOEI4JDOKJ2BXJI5P0BIA.1</docID>
    <addDate>2011-10-28T12:26:00</addDate>
  </Document>
</Documents>

Now what I need to do is to add a DispName attribute to the element MVtext. Attribute cannot have any spaces and I would like to include the friendly name e.g. Multi Value Text.

like image 806
paparazzo Avatar asked Dec 13 '11 03:12

paparazzo


2 Answers

Try something like this (untested, since I don't have your database tables to test against...):

  SELECT 
     sv.sID AS '@sID',
     sv.docID AS 'docID',
     (SELECT 
         value AS 'value'
      FROM   
         dbo.docMVtext mv
      WHERE
         mv.sID = sv.sID
      ORDER BY mv.value
      FOR XML PATH (''), TYPE) AS 'To'    
  FROM   
      dbo.docSVsys sv
  WHERE  
      sv.sID = '57'
  ORDER BY 
      sv.sID
  FOR XML PATH('Document')

Does that give you what you're looking for?? And don't you agree with John and me: this is much simpler than FOR XML EXPLICIT.....

like image 111
marc_s Avatar answered Oct 12 '22 08:10

marc_s


From Examples: Using PATH Mode:

USE AdventureWorks2008R2;
GO
SELECT ProductModelID AS "@ProductModelID",
      Name AS "@ProductModelName",
      (SELECT ProductID AS "data()"
       FROM   Production.Product
       WHERE  Production.Product.ProductModelID = 
              Production.ProductModel.ProductModelID
       FOR XML PATH ('')
       ) AS "@ProductIDs",
       (
       SELECT Name AS "ProductName"
       FROM   Production.Product
       WHERE  Production.Product.ProductModelID = 
              Production.ProductModel.ProductModelID
       FOR XML PATH (''), type
       ) AS "ProductNames"

FROM Production.ProductModel
WHERE ProductModelID= 7 OR ProductModelID=9
FOR XML PATH('ProductModelData');
like image 43
John Saunders Avatar answered Oct 12 '22 08:10

John Saunders