2011-12-13 85 views
5

沒能得到想要的XML輸出TSQL FOR XML EXPLICIT

以下:

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> 

現在我需要做的是一個DispName屬性添加到元素MVtext。屬性不能有任何空格,我希望包含友好名稱,例如多值文本。

+4

你不應該使用FOR XML EXPLICIT。這是一場噩夢。您正在使用SQL Server 2008,因此請使用「FOR XML PATH」。 –

+0

@JohnSaunders OK John,給我個提示。我厭倦了XML PATH,並沒有得到任何地方。 – Paparazzi

+0

[提示](http://msdn.microsoft.com/en-us/library/ms189885.aspx) –

回答

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

這是否能給你帶來什麼您正在尋找??你不同意約翰和我:這是簡單得多比​​.....

+0

當它不工作時,它很難。你的代碼很接近。在第三行末尾只有一個逗號。我現在有一個相關的帖子,關於如何獲取單個值屬性到'To' – Paparazzi

+0

哦,在我發佈這個問題之前,我差不多在那裏。我錯過了「,TYPE」。 – Paparazzi

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