TSQL for XML EXPLICIT

时间:2011-12-13 03:19:36

标签: sql-server xml sql-server-2008 tsql sqlxml

无法获得所需的XML输出

以下内容:

  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;

制作:

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

我想要的是:

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

我可以使用FOR XML获取该输出吗?

好的,我知道他们在技术上可能是等同的 我想要的和我需要的是不一样的。

使用xDocument这是缓慢的 有数百万个文档,并且需要XML一次最多100万个XML TSQL FOR XML非常快 我只需要让FOR XML格式化。

解决方案(基于已接受的答案):

   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')

产地:

<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>

现在我需要做的是向元素MVtext添加DispName属性。属性不能有任何空格,我想包含友好名称,例如多值文本。

2 个答案:

答案 0 :(得分:3)

尝试这样的事情(未经测试,因为我没有数据库表来测试......):

  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')

这会给你什么吗?你不同意John和我的意见:这比<{1}} 简单得多 .....

答案 1 :(得分:2)

来自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');